Home > Back-end >  Python dataframe: copy index column into a new column (error)
Python dataframe: copy index column into a new column (error)

Time:11-07

I have a dataframe (with the first column being index column)(below). I would like to add(create) a (last) column which is a copy of the index column (desired result below). However I have the error (below). Is there a workaround? Thanks in advance

import pandas as pd
df1 = pd.DataFrame({"date": ['2021-3-22', '2021-3-23', '2021-3-24', '2021-3-25', '2021-3-26'],
"x": ['nan', 1, 'nan', 'nan', 'nan' ]})
df1.set_index('date', inplace=True)
df1

date        x
2021-3-22   nan
2021-3-23   1
2021-3-24   nan
2021-3-25   nan
2021-3-26   nan

df1['date1'] = df1['date'].copy()
df1

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3360             try:
-> 3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:

~\anaconda3\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

~\anaconda3\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'date'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
C:\Temp/ipykernel_10224/2516020320.py in <module>
----> 1 df1['date1'] = df1['date'].copy()
      2 df1

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   3456             if self.columns.nlevels > 1:
   3457                 return self._getitem_multilevel(key)
-> 3458             indexer = self.columns.get_loc(key)
   3459             if is_integer(indexer):
   3460                 indexer = [indexer]

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:
-> 3363                 raise KeyError(key) from err
   3364 
   3365         if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 'date'



The desired result is: 

    date        x   date1
0   2021-3-22   nan 2021-3-22
1   2021-3-23   1   2021-3-23
2   2021-3-24   nan 2021-3-24
3   2021-3-25   nan 2021-3-25
4   2021-3-26   nan 2021-3-26

Many thanks in advance!

CodePudding user response:

df1.index instead df1['date'] :

df1['date1'] = df1.index.copy()
df1.reset_index()

output:

    date        x   date1
0   2021-3-22   nan 2021-3-22
1   2021-3-23   1   2021-3-23
2   2021-3-24   nan 2021-3-24
3   2021-3-25   nan 2021-3-25
4   2021-3-26   nan 2021-3-26

CodePudding user response:

To keep the date column adapt your solution and add drop=False to set_index().

import pandas as pd
df1 = pd.DataFrame({"date": ['2021-3-22', '2021-3-23', '2021-3-24', '2021-3-25', '2021-3-26'],
"x": ['nan', 1, 'nan', 'nan', 'nan' ]})
df1.set_index('date', inplace=True, drop=False)
df1
>>>              date     x
date        
2021-3-22   2021-3-22   nan
2021-3-23   2021-3-23   1
2021-3-24   2021-3-24   nan
2021-3-25   2021-3-25   nan
2021-3-26   2021-3-26   nan

With this change you can use df1['date1'] = df1['date'].copy() or simply rename date to date1 with df1.rename({'date':'date1'}, axis=1).

df1 = df1.rename({'date':'date1'}, axis=1)
df1
>>>             date1     x
date        
2021-3-22   2021-3-22   nan
2021-3-23   2021-3-23   1
2021-3-24   2021-3-24   nan
2021-3-25   2021-3-25   nan
2021-3-26   2021-3-26   nan
  • Related