当数据中的特定条目未使用replace方法更新时,如何在数据框中使用replace()替换列中的特定条目?

时间:2020-06-03 10:16:15

标签: python pandas jupyter-notebook data-science

Here is a dataframe named Energy which looks like this:       
    Country                  Energy Supply      Energy Supply per Capita
16  Afghanistan              3.210000e+08       10.0    
17  Albania                  1.020000e+08       35.0    
18  Algeria                  1.959000e+09       51.0    
19  American Samoa           NaN                                        
40  Bolivia                  3.360000e+08       32.0
... ... ... ...
180 Republic of Korea        1.100700e+10       221.0   
213 Switzerland              1.113000e+09       136.0   
214 Syrian Arab Republic     5.420000e+08       28.0    
215 Tajikistan               1.060000e+08       13.0    
216 Thailand                 5.336000e+09       79.0    
228 Ukraine                  4.844000e+09       107.0
230 United Kingdom of 
    Great Britain and 
    Northern I...            7.920000e+09       124.0   
231 United Republic of Tanzania 9.940000e+08    20.0    
232 United States of America    9.083800e+10    286.0       

Energy=pd.read_excel('Energy Indicators.xls')
Energy=Energy[(Energy.index>15)&(Energy.index<243)]

I needed to replace name of countries like "United States of America" to 
"United States","United Kingdom of Great Britain and Northern Ireland" to 
"United Kingdom".  
I used the following code:

Energy['Country']=Energy.Country.str.strip('123.!?\n\t')
Energy['Country'].replace({'United States of America':'United 
States','United Kingdom of Great Britain and Northern Ireland':'United 
Kingdom'},inplace=True)

但是国家名称未在输出中更新。我不知道哪里错了。 无论我是否使用拆分方法,都无法正常工作。
有人请帮忙。

0 个答案:

没有答案