我试图重现pandasuser guide中的“轴上的基本索引和多索引”部分
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
print(df)
print(df['bar'])
我看不出为什么我的代码与用户指南中显示的代码有什么不同。我有一个键错误:最后一行是“bar”
您所参考的用户指南(this)部分是针对数据帧的列是多索引的情况。当索引为多索引时,您应该查看此part
要访问
bar
,您应该执行以下操作:如果要访问
bar
,则需要使用loc
,或将其转置,因为bar
不在列中:转置:
编辑:
如前所述,在注释中非常有用,如果您想保留多索引:
尽管这取决于您的用例,但看起来您实际上需要:
参考资料:
^{}
^{}
^{}
你问题中的链接。我建议你仔细检查一下,解释得很好
相关问题 更多 >
编程相关推荐