我想按以下方式更改数据:

"Republic of Korea": "South Korea",
"United States of America": "United States",
"United Kingdom of Great Britain and Northern Ireland": "United Kingdom",
"China, Hong Kong Special Administrative Region": "Hong Kong"


当前,我正在使用代码:

energy.Country[energy.Country == "Republic of Korea"] = "South Korea"
energy.Country[energy.Country == "United States of America"] = "United States"
energy.Country[energy.Country == "United
               Kingdom of Great Britain and Northern Ireland"] = "United Kingdom"
energy.Country[energy.Country == "China,
               Hong Kong Special Administrative Region"] ="Hong Kong"`


我尝试通过将参数作为字典传递来使用.replace方法执行相同的操作:

energy.replace('Country' : {"Republic of Korea": "South Korea","United States of America": "United States","United Kingdom of Great Britain and Northern Ireland": "United Kingdom","China, Hong Kong Special Administrative Region": "Hong Kong"})

但这似乎不起作用,是否有更干净整洁的方法来做到这一点?

最佳答案

replace上调用Series,这更容易。

repl_dict = {"Republic of Korea": "South Korea", ...}
energy['Country'] = energy['Country'].replace(repl_dict)


请注意,这不是使用map的好地方,因为“国家”中未映射到repl_dict中任何内容的条目将替换为NaN。



另一个选择是基于列表补偿的替换:

energy['Country'] = [
    repl_dict.get(x, x) for x in energy['Country'].tolist()]


不如replace简洁,但在性能方面绝对胜任。

关于python - 在一行代码中用 Pandas 执行多次替换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52918274/

10-12 20:19