【问题标题】:Python: "The label is not in the columns" error after pd.read_csv(..., index_col='year')Python:pd.read_csv(..., index_col='year') 后出现“标签不在列中”错误
【发布时间】: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) 得到你想要的情节而不分配系列dfyeardfridership 我想(看不到情节或我不能说的数据当然)。
  • 因为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


【解决方案1】:

我认为year 是您的索引。试试dfyear = df.index 而不是dfyear = df.loc[:,'year']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    相关资源
    最近更新 更多