Python Pandas - KeyError: "None of [Index([...] are in the [columns])

Python Pandas - KeyError: "None of [Index([...] are in the [columns])

尝试将一个简单的 csv 文件变为红色,然后尝试可视化数据框索引中的一个变量(这是一个日期)。但它只是不断抛出关键错误。通读所有有类似问题的帖子,但 none 似乎适用于我的情况。非常感谢任何帮助。

df1 = pd.read_csv('df1',index_col=0)
df1.head()

    A   B   C   D
2000-01-01  1.339091    -0.163643   -0.646443   1.041233
2000-01-02  -0.774984   0.137034    -0.882716   -2.253382
2000-01-03  -0.921037   -0.482943   -0.417100   0.478638
2000-01-04  -1.738808   -0.072973   0.056517    0.015085
2000-01-05  -0.905980   1.778576    0.381918    0.291436

在此之后,我只是尝试将任何一个变量绘制为折线图以供索引

df1.plot.line(x=df1.index, y='A', figsize=(12,3), lw=1)

错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-23-531889937593> in <module>
----> 1 df1.plot.line(x=df1.index,y='A',figsize=(12,3),lw=1)

~\Anaconda3\lib\site-packages\pandas\plotting\_core.py in line(self, x, y, **kwargs)
   1021         as coordinates.
   1022         """
-> 1023         return self(kind="line", x=x, y=y, **kwargs)
   1024 
   1025     @Appender(

~\Anaconda3\lib\site-packages\pandas\plotting\_core.py in __call__(self, *args, **kwargs)
    918                 if is_integer(x) and not data.columns.holds_integer():
    919                     x = data_cols[x]
--> 920                 elif not isinstance(data[x], ABCSeries):
    921                     raise ValueError("x must be a label or position")
    922                 data = data.set_index(x)

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   3028             if is_iterator(key):
   3029                 key = list(key)
-> 3030             indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
   3031 
   3032         # take() does not accept boolean indexers

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1264             keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
   1265 
-> 1266         self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
   1267         return keyarr, indexer
   1268 

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1306             if missing == len(indexer):
   1307                 axis_name = self.obj._get_axis_name(axis)
-> 1308                 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
   1309 
   1310             ax = self.obj._get_axis(axis)

KeyError: "None of [Index(['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04', '2000-01-05',\n       '2000-01-06', '2000-01-07', '2000-01-08', '2000-01-09', '2000-01-10',\n       ...\n       '2002-09-17', '2002-09-18', '2002-09-19', '2002-09-20', '2002-09-21',\n       '2002-09-22', '2002-09-23', '2002-09-24', '2002-09-25', '2002-09-26'],\n      dtype='object', length=1000)] are in the [columns]"

虽然这不会直接影响我的工作,但我当然想知道为什么会这样。提前致谢!!

不幸的是 plot 只接受列名,因此您应该重置索引:

df1.reset_index().plot.line(x='index', y='A', figsize=(12,3), lw=1)

您也可以按照文档的建议省略 x 参数:

xlabel or position, optional.
Allows plotting of one column versus another. If not specified, the index of the DataFrame is used