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

Time:09-27

Trying to red a simple csv file and then trying to visualize one of the variables on the index of the data-frame (it's a date). But it just keeps throwing back the key error. Read through all the posts with similar problems, but none seems to work in my case. Any help is much appreciated.

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

After this I am simply trying to draw any one variable as a line chart wrt to index

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

Error:

---------------------------------------------------------------------------
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]"

Though this does not impact my work directly, but I will certainly like to know why this is happening. Thanks in advance!!

CodePudding user response:

Unfortunately plot only takes column names, so you should instead reset the index:

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