【发布时间】:2018-08-11 05:39:21
【问题描述】:
我正在尝试访问我的数据框中的一列,但它不起作用
import pandas as pd
df = pd.read_csv(data, index_col='year')
dfyear = df.loc[:,'year']
我收到以下错误:
KeyError: 'the label [year] is not in the [columns]'
我该如何解决这个问题?
【问题讨论】:
-
您能
print(df.head(5))向我们展示您正在处理的数据吗? -
旁注:你可以只做
df.plot(y='average_ridership', use_index=True)得到你想要的情节而不分配系列dfyear或dfridership我想(看不到情节或我不能说的数据当然)。 -
因为
pd.read_csv(..., index_col='year')是explicitly telling it to drop 'year' as a column and use it for row-indices,正如文档所说。如果你不想那样做,那就不要那样做。只需阅读“年份”以及其他列即可。这几乎是许多现有问题的重复。 -
试试 df.index 因为我猜年份是一个索引
标签: python pandas matplotlib