尝试创建一个简单的csv文件,然后尝试在数据帧的索引上可视化变量之一(它是一个日期)。但它总是抛出键错误。通读了所有有类似问题的帖子,但似乎没有一个在我的情况下工作。如有任何帮助,不胜感激。
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
在此之后,我只是试图绘制任何一个变量作为折线图wrt索引
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)
~Anaconda3libsite-packagespandasplotting_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(
~Anaconda3libsite-packagespandasplotting_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)
~Anaconda3libsite-packagespandascoreframe.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
~Anaconda3libsite-packagespandascoreindexing.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
~Anaconda3libsite-packagespandascoreindexing.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]"
虽然这不会直接影响我的工作,但我肯定想知道为什么会发生这种情况。提前感谢!!
您也可以像文档建议的那样省略x
参数:
xlabel或position,可选。
允许绘制一列与另一列之间的关系。如果未指定,则使用DataFrame的索引
不幸的是,plot
只接受列名,所以您应该重置索引:
df1.reset_index().plot.line(x='index', y='A', figsize=(12,3), lw=1)