我正在使用python pandas进行数据分析,我想在数据框中更改系列的名称。
这很有效,但效果似乎很低:
AA = pandas.DataFrame( A )
for series in A:
AA[A_prefix+series] = A[series]
del A[series]
有没有办法更改系列名称?
答案 0 :(得分:8)
当然,您可以使用rename方法:
In [11]: df = DataFrame({"A": [1,2], "B": [3,4]})
In [12]: df.rename(columns={"A": "series formerly known as A"})
Out[12]:
series formerly known as A B
0 1 3
1 2 4
但这不会改变df
:
In [13]: df
Out[13]:
A B
0 1 3
1 2 4
如果您想使用inplace
:
In [14]: df.rename(columns={"A": "series formerly known as A"}, inplace=True)
Out[14]:
series formerly known as A B
0 1 3
1 2 4
In [15]: df
Out[15]:
series formerly known as A B
0 1 3
1 2 4