1

我正在使用 python pandas 进行数据分析,并且我想更改数据框中的系列名称。

这行得通,但似乎效率很低:

AA = pandas.DataFrame( A )
for series in A:
    AA[A_prefix+series] = A[series]
    del A[series]

有没有办法更改系列名称?

4

1 回答 1

8

当然,您可以使用重命名方法:

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
于 2012-08-30T16:14:27.407 回答