如何使用Pandas中的索引编号重命名列标题

时间:2015-11-20 04:28:42

标签: python pandas

我有以下数据框

import pandas as pd
df = pd.DataFrame({ 'gene':["foo",
                            "lal",
                            "qux",
                            "woz"], 'cell1':[5,9,1,7], 'cell2':[12,90,13,87]})
df = df[["gene","cell1","cell2"]]
df

看起来像这样:

  gene  cell1  cell2
0  foo      5     12
1  lal      9     90
2  qux      1     13
3  woz      7     87

我想要做的是更改第1列和第3列中的列名。 导致:

    X  cell1   Y
  foo      5  12
  lal      9  90
  qux      1  13
  woz      7  87

如何使用02索引编号来完成此操作。

我可以这样做

df.columns = ["X","cell1","Y"]

但它不使用列索引。

2 个答案:

答案 0 :(得分:4)

从列中创建列表,更改列表,然后将列表重新分配给columns属性:

>>> cols = list(df.columns)
>>> cols[0] = 'X'
>>> cols[2] = 'Y'
>>> df.columns = cols

或者,单行:

>>> df.rename(columns={'cell1': 'X', 'gene': 'Y'}, inplace=True)

答案 1 :(得分:2)

df.columns._data[0] = 'X'

df.columns._data[2] = 'Y'

>>> df
     X  cell1   Y
0  foo      5  12
1  lal      9  90
2  qux      1  13
3  woz      7  87

一般说明:如有疑问,请查看班级__dict__变量:

>>> df.columns.__dict__
{'freq': None, '_cache': {'dtype': dtype('O'), 'is_all_dates': False, 'is_unique': True, 'inferred_t
ype': 'string', '_engine': <pandas.index.ObjectEngine object at 0x000000000882DC48>}, '_data': array
(['gene', 'cell1', 'cell2'], dtype=object), '_id': <object object at 0x00000000028F4720>, 'name': No
ne}