AttributeError: 'collections.OrderedDict' object has no attribute 'iloc'

一笑奈何 提交于 2021-01-29 21:30:55

问题


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:


  1. first of all please put the exact error message.
  2. print(type(df)) it should return <class 'pandas.core.frame.DataFrame'> not 'collections.OrderedDict'
  3. while reading mention sheetname pd.read_excel(file.xlsx, sheet_name=sheet1) or pd.read_excel(file.xlsx, sheet_name=None) if no sheet name provided
  4. Please go through documentation of any modules before using it


来源:https://stackoverflow.com/questions/59213846/attributeerror-collections-ordereddict-object-has-no-attribute-iloc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!