首页 > 解决方案 > Python Pandas 'head' 属性不起作用

问题描述

我正在使用 python 和 pandas 编写一个快速脚本来处理一些数据,但是,我正在尝试打印,'head'但是当我这样做时出现错误。任何想法可能是什么问题?

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

import pandas as pd
my_file_location = "whatever.xlsx"
dfs = pd.read_excel(my_file_location, sheet_name=None)

print(dfs.head())

标签: pythonpandas

解决方案


您正在加载一个带有多个工作表的工作簿,并且您传递sheet_name = Nonepd.read_excel()which 告诉它加载工作簿中的所有工作表并将它们作为字典返回。从字典中选择一张工作表,或将工作表的名称传递给pd.read_excel().

例如,要显示所有工作表:

import pandas as pd
my_file_location = "whatever.xlsx"
dfs = pd.read_excel(my_file_location, sheet_name=None)
for n,d in dfs.items():
  print('Sheet Name:{}'.format(n))
  print(d.head())

或选择名为“somename”的工作表:

import pandas as pd
my_file_location = "whatever.xlsx"
dfs = pd.read_excel(my_file_location, sheet_name='somename')
print(d.head())

更多选项在文档中解释pandas.read_excel()


推荐阅读