为什么我不能在重命名后访问pandas中的列?

时间:2017-07-14 12:53:21

标签: python python-2.7 pandas

如果我有一个数据框并重命名该列,我将无法通过其新名称访问该列。

参见示例:

df.columns.values[0] = 'newname'
df
   newname   b
0        1  10
1        2  20

现在,如果我按照建议here的方式重命名列'a'。

 df['newname']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/gpfs0/export/opt/anaconda-2.3.0/lib/python2.7/site-packages/pandas/core/frame.py", line 1797, in __getitem__
    return self._getitem_column(key)
  File "/gpfs0/export/opt/anaconda-2.3.0/lib/python2.7/site-packages/pandas/core/frame.py", line 1804, in _getitem_column
    return self._get_item_cache(key)
  File "/gpfs0/export/opt/anaconda-2.3.0/lib/python2.7/site-packages/pandas/core/generic.py", line 1084, in _get_item_cache
    values = self._data.get(item)
  File "/gpfs0/export/opt/anaconda-2.3.0/lib/python2.7/site-packages/pandas/core/internals.py", line 2851, in get
    loc = self.items.get_loc(item)
  File "/gpfs0/export/opt/anaconda-2.3.0/lib/python2.7/site-packages/pandas/core/index.py", line 1572, in get_loc
    return self._engine.get_loc(_values_from_object(key))
  File "pandas/index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas/index.c:3824)
  File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:3704)
  File "pandas/hashtable.pyx", line 686, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12280)
  File "pandas/hashtable.pyx", line 694, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12231)
KeyError: 'newname'

现在让我们尝试使用'newname'

访问该列
df['a']
0    1
1    2
Name: a, dtype: int64

然而,我仍然可以通过旧名称访问该列。

Route-1

似乎我已经更改了列的名义名称,但该更改没有传播到用于在数据框结构中使用deferred列的字典。

问题:为什么会出现这种情况以及如何解决?

1 个答案:

答案 0 :(得分:1)

你可以使用这种方法:

In [131]: df.columns = ['newname'] + df.columns.tolist()[1:]

In [132]: df
Out[132]:
   newname   b
0        1  10
1        2  20

或:

In [136]: df = df.rename(columns={df.columns.tolist()[0]:'newname'})

In [137]: df
Out[137]:
   newname   b
0        1  10
1        2  20