Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python的“head”属性不起作用_Python_Pandas - Fatal编程技术网

Python的“head”属性不起作用

Python的“head”属性不起作用,python,pandas,Python,Pandas,我正在使用python和pandas编写一个快速脚本来处理一些数据,但是,我正在尝试打印“head”,但是当我打印时,会出现一个错误。你知道有什么问题吗 AttributeError:“collections.OrderedDict”对象没有属性“head” 您正在加载包含多张工作表的工作簿,并将sheet_name=None传递给pd.read_excel,这会告诉它加载工作簿中的所有工作表并将其作为字典返回。从字典中选择一张工作表,或将工作表的名称传递给pd.read\u excel 例如,

我正在使用python和pandas编写一个快速脚本来处理一些数据,但是,我正在尝试打印“head”,但是当我打印时,会出现一个错误。你知道有什么问题吗

AttributeError:“collections.OrderedDict”对象没有属性“head”


您正在加载包含多张工作表的工作簿,并将sheet_name=None传递给pd.read_excel,这会告诉它加载工作簿中的所有工作表并将其作为字典返回。从字典中选择一张工作表,或将工作表的名称传递给pd.read\u 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())

更多选项将在

中解释,并缩短Craig的解决方案:

import pandas as pd
my_file_location = "whatever.xlsx"
dfs = pd.read_excel(my_file_location, sheet_name=None)
[(print('Sheet Name:{}'.format(n)),print(d.head())) for n,d in dfs.items()]
在列表理解中使用打印

代码中的问题是因为dfs是一系列具有head属性的对象:-有趣的解释方式

如果图纸仅包含一张图纸:

...
print(dfs[0].head())

我还要补充一点,如果你知道你的.xlsx只有一张工作表,你可以使用“dfs[0]”而不是循环。奇怪的是,当我尝试添加工作表名称时,我得到一个错误,说没有具有该名称的工作表。在列表中使用print是不必要的。不确定是否有意义:@RafaelC很明显,这是不必要的,我这样做是为了延长时间:-hhihihhha
...
print(dfs[0].head())