问题
import pandas as pd
file = 'D:/myproject/chatbot_database.xlsx'
xl = pd.read_excel(file)
print(xl) #this prints fine
print(xl.iloc[0, 1]) #this throws an error
The error message is:
AttributeError: 'collections.OrderedDict' object has no attribute 'iloc'
I have tried searching through stackoverflow and googled this for 2 days but i just don't seem to be able to get an answer so i am posting this question. So the main issue is that this code works on my computer but it does not work on my friends computer. I tried to reinstall pandas hoping that that would fix the iloc issue since iloc is part of pandas but no avail. Anyone has any idea what the issue is? or what other libraries may be missing?
Edited post below with screenshot below
回答1:
Reason is you forget mentioned sheet_name=None parameter in read_excel, what return OrderedDict, where keys are sheetnames and values are DataFames:
sheet_name : str, int, list, or None, default 0
Strings are used for sheet names. Integers are used in zero-indexed sheet positions. Lists of strings/integers are used to request multiple sheets. Specify None to get all sheets.
Available cases:
Defaults to 0: 1st sheet as a DataFrame
1: 2nd sheet as a DataFrame
"Sheet1": Load sheet with name “Sheet1”
[0, 1, "Sheet5"]: Load first, second and sheet named “Sheet5” as a dict of DataFrame
None: All sheets.
xl = pd.read_excel(file, sheet_name=None)
回答2:
- first of all please put the exact error message.
- print(type(df)) it should return
<class 'pandas.core.frame.DataFrame'>not'collections.OrderedDict' - while reading mention sheetname
pd.read_excel(file.xlsx, sheet_name=sheet1)orpd.read_excel(file.xlsx, sheet_name=None)if no sheet name provided - Please go through documentation of any modules before using it
来源:https://stackoverflow.com/questions/59213846/attributeerror-collections-ordereddict-object-has-no-attribute-iloc