我有一个数据框(第一列是索引列)(下面)。我想添加(创建)一个(最后)列,这是索引列的副本(下面期望的结果)。然而,我有错误(如下)。有解决办法吗?提前感谢
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)
~anaconda3libsite-packagespandascoreindexesbase.py in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
~anaconda3libsite-packagespandas_libsindex.pyx in pandas._libs.index.IndexEngine.get_loc()
~anaconda3libsite-packagespandas_libsindex.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas_libshashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas_libshashtable_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
~anaconda3libsite-packagespandascoreframe.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]
~anaconda3libsite-packagespandascoreindexesbase.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
提前感谢!
df1.index
代替df1['date']
:
df1['date1'] = df1.index.copy()
df1.reset_index()
输出: 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
为了保持date
列,调整您的解决方案并将drop=False
添加到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
你可以使用df1['date1'] = df1['date'].copy()
或者用df1.rename({'date':'date1'}, axis=1)
将date
重命名为date1
。
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