pandas read_excel(sheet name = None) returns a dictionary of strings, not dataframes?

2024/10/11 8:25:27

The pandas read_excel documentation says that specifying sheet_name = None should return "All sheets as a dictionary of DataFrames". However when I try to use it like so I get a dictionary of strings.

target_file = "C:\file_here.xlsx"
data = pd.read_excel(target_file) 
print(type(data))
data = pd.read_excel(target_file, sheet_name = None)
print(type(data))
#print(data)
for sheet in data:print(type(sheet))

This returns:

<class 'pandas.core.frame.DataFrame'>
<class 'collections.OrderedDict'>
<class 'str'>

I don't understand why the latter returns strings. I want to be able to access each sheet as a dataframe, perform a string replace on that sheet(dataframe), and then put those sheets to a new xlsx file using to_excel. But I'm confused why for sheet in data: is returning strings.

If I print data (the ordered dictionary) below in the following snippet, it prints the dataframes to console. So it looks like I'm not accessing the dictionary correctly, but I'd like to understand what I'm doing wrong exactly:

data = pd.read_excel(target_file, sheet_name = None)
print(type(data))
print(data)
Answer

data = pd.read_excel(target_file, sheet_name = None) returns a Dict. When you loop over data you get the keys of the Dict.

Try:

for key in data:print(data[key].head())
https://en.xdnf.cn/q/69790.html

Related Q&A

Plotly: How to assign specific colors for categories? [duplicate]

This question already has an answer here:How to define colors in a figure using Plotly Graph Objects and Plotly Express(1 answer)Closed 2 years ago.I have a pandas dataframe of electricity generation m…

Using nested asyncio.gather() inside another asyncio.gather()

I have a class with various methods. I have a method in that class something like :class MyClass:async def master_method(self):tasks = [self.sub_method() for _ in range(10)]results = await asyncio.gath…

AttributeError: type object Word2Vec has no attribute load_word2vec_format

I am trying to implement word2vec model and getting Attribute error AttributeError: type object Word2Vec has no attribute load_word2vec_formatBelow is the code :wv = Word2Vec.load_word2vec_format("…

Python - Core Speed [duplicate]

This question already has answers here:Getting processor information in Python(12 answers)Closed 8 years ago.Im trying to find out where this value is stored in both windows and osx, in order to do som…

App Engine, transactions, and idempotency

Please help me find my misunderstanding.I am writing an RPG on App Engine. Certain actions the player takes consume a certain stat. If the stat reaches zero the player can take no more actions. I start…

Speed differences between intersection() and object for object in set if object in other_set

Which one of these is faster? Is one "better"? Basically Ill have two sets and I want to eventually get one match from between the two lists. So really I suppose the for loop is more like:f…

Pandas.read_csv reads all of the file into one column

I have a csv file in the form "...","...","..."... with over 40 columns. When I used this simple code, it only gives me one massive key. Ive been messing with it for over …

Python lazy evaluation numpy ndarray

I have a large 2D array that I would like to declare once, and change occasionnaly only some values depending on a parameter, without traversing the whole array. To build this array, I have subclassed …

Python 2.7 NetworkX (Make it interactive)

I am new to NetworkX. Right now, I manage to connect all the nodes to this particular node. What I want to do next it to make it interactive e.g. able to make each of the node move by dragging using cu…

Normal Distribution Plot by name from pandas dataframe

I have a dataframe like below:dateTime Name DateTime day seconds zscore 11/1/2016 15:17 james 11/1/2016 15:17 Tue 55020 1.158266091 11/1/2016 13:41 james 11/1/2016 13:41 Tue 4926…