gpt4 book ai didi

python - 如何根据某些条件更改 pandas 中的列值

转载 作者:行者123 更新时间:2023-12-01 00:43:56 24 4
gpt4 key购买 nike

当数据框中的列值匹配某些特定条件时,我想将其更改为另一个名称。

我尝试在数据帧上应用方法,但没有成功。

这是我拥有的数据集,我希望在某些情况下应更改国家/地区列名称。例如,“韩国”应更改为“韩国”,我有类似的名称需要在此栏中更改。我尝试了应用方法,但没有得到任何结果。任何建议都会对我有帮助。先感谢您。**

df.head()

Country Energy_Supply Energy_Supply_per_Capita
0 Afghanistan 3.210000e 10.0
1 Albania 1.020000e 35.0
2 Algeria 1.959000e+09 51.0
3 American Samoa NaN NaN

最佳答案

您可以使用replace()为您感兴趣的专栏。创建字典 repl_dict = {"中华民国": "中国", "摩尔多瓦共和国": "摩尔多瓦", "法兰西共和国": "法国","英国": "英格兰"} 然后pass就是替换函数,更多详情和参数查看pandas.DataFrame.replace

This method does replace all at once by creating the dictionary with all the names of interest, ultimately will standardize your dataframe column based on this dictionary and no need to run individually for each Country.

import pandas as pd
my_dict = { 'Country' : ["Republic of China", "China", "England", "Republic of Moldova", "Republic of France","Great Britain", "England"],
'age' : [20,27, 35, 55, 18, 21, 35],
'designation': ["VP", "CEO", "CFO", "VP", "VP", "CEO", "MD"]}

dfnew = pd.DataFrame(my_dict)
print(dfnew)

Country age designation
0 Republic of China 20 VP
1 China 27 CEO
2 England 35 CFO
3 Republic of Moldova 55 VP
4 Republic of France 18 VP
5 Great Britain 21 CEO
6 England 35 MD
repl_dict = {"Republic of China": "China", "Republic of Moldova": "Moldova", "Republic of France": "France","Great Britain": "England"}
dfnew['Country'] = dfnew['Country'].replace(repl_dict, regex=True)
print()
print('Final dataframe', dfnew)
Final dataframe    
Country age designation
0 China 20 VP
1 China 27 CEO
2 England 35 CFO
3 Moldova 55 VP
4 France 18 VP
5 England 21 CEO
6 England 35 MD

关于python - 如何根据某些条件更改 pandas 中的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57134515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com