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

标签 python pandas

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

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

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

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/

相关文章:

python - 如何在 CentOS 7 上专门为 Python3 安装 pip?

python - 为 vim 添加 python 支持

python - 为另一列中的唯一值分配值给 Pandas 数据框值

python - 基于第三列的列匹配

pandas - 如何使用 Pandas 在整个数据框中搜索部分字符串?

Python selenium is_displayed 方法

python - 扩展 ASCII 字符转换

python - 将数据类型系列转换为日期时间

python - 将组分配给 Pandas 列中的连续 1

python - Pandas 合并数据帧,覆盖键上的值