2014-10-03 13 views
1

我有一個DF它看起來像這樣:Python的熊貓:如果在列標籤找到替代定的字符

Label      PG & PCF  EE-LV Centre UMP  FN 
Label                  
Très favorable     2.68  3.95 4.20 3.33 3.05 
Plutôt favorable    12.45  42.10 19.43 2.05 1.77 
Plutôt défavorable    43.95  41.93 34.93 20.15 15.97 
Très défavorable    37.28  9.11 41.44 70.26 75.99 
Je ne sais pas     3.63  2.91 0.10 4.21 3.22 

我只是想取代「&」與「和」裏發現,無論是在列標籤或索引標籤。

我會一直拍攝這樣的事情wouldve工作,但它沒有...

dataframe.columns.replace("&","and") 

任何想法?

回答

1

什麼你試圖將不是Index工作,有沒有這樣的屬性,但我們可以做的是轉換列成系列,然後使用strreplace做你想做什麼,你應該能夠做到analagous對指數操作太:

df.columns = pd.Series(df.columns).str.replace('&', 'and') 
df 
Out[307]: 
        PG and PCF EE-LV Centre UMP 
Label            
Très favorable   2.68 3.95 4.20 3.33 
Plutôt favorable   12.45 42.10 19.43 2.05 
Plutôt défavorable  43.95 41.93 34.93 20.15 
Très défavorable   37.28 9.11 41.44 70.26 
Je ne sais pas   3.63 2.91 0.00 4.21