2012-08-30 129 views
1

我正在使用python熊貓進行數據分析,我想更改數據框中一系列的名稱。python熊貓:重命名數據框中的一個系列?

這工作,但似乎非常低效:

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

有沒有辦法到位,以改變該系列的名稱?

回答

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 
相關問題