Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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 can';t访问df.set\u索引后的列_Python_Pandas - Fatal编程技术网

Python can';t访问df.set\u索引后的列

Python can';t访问df.set\u索引后的列,python,pandas,Python,Pandas,hello在我的数据框的一列上设置了索引之后,我想提取属于该列的数据,但是python向我显示了以下错误KeyError:“airline”。航空公司是本专栏的标题。谢谢你帮助我 这是为了可视化我的数据帧 df\u air.set\u索引('airline',inplace=True) 打印(df_air.loc['Royal air Maroc']) subDf=df_air[df_air['airline'].isin(['埃及航空公司','Royal air Maroc','Saudi A

hello在我的数据框的一列上设置了索引之后,我想提取属于该列的数据,但是python向我显示了以下错误KeyError:“airline”。航空公司是本专栏的标题。谢谢你帮助我

这是为了可视化我的数据帧

df\u air.set\u索引('airline',inplace=True)
打印(df_air.loc['Royal air Maroc'])
subDf=df_air[df_air['airline'].isin(['埃及航空公司','Royal air Maroc','Saudi Arabian'])]
subDf
2 first命令工作得很好,但当我执行subDf时,它会显示以下错误:

KeyError: 'airline', 
我试图重置我的索引,我的问题解决了,所以问题是关于索引的


如何在不重置索引的情况下同时使用这两个字段?

airline
不再是列,因此无法通过这种方式访问。但是,您应该仍然能够像这样创建子集

df_air.loc[['Egyptair', 'Royal Air Maroc','Saudi Arabian']]

如果你将它设置为索引,那么它当然不再是一列。在你将一列设置为索引后,通过
df.index
而不是
df['column\u name']
i解决了我的问题:subDf=df_air[df_air.index.to_series().isin(['埃及航空','Royal air Maroc','Saudi Arabian'))subdft在这里使用
.loc
方法将它们视为标签,因此这将返回具有这些标签的行。如果你是对的,你可以读更多。更好的解决方案是按照您的建议,使用
df.index.isin([…])
进行切片;)我使用df.index解决了这个问题,正如前面thnx所说的那样。对于['Egyptair'、'Royal Air Maroc'、'Saudi Arabian',这些是我的航空公司专栏中的一些行,它们不是专栏