I want to change the column values in a dataframe to another name when it matches some certain conditions.
I tried apply method on the dataframe but it didnt work.
This is the dataset i have and i want the country column names should be changed on some condition. For example “Republic of Korea” should be change to ‘South Korea’ and I have similar names that needs to be changed in this column. I tried apply method and i’m not getting any results. Any suggestions would help me. Thank you in advance. **
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
Answer
You can use replace() for your column of interest.
Create a dictionary repl_dict = {"Republic of China": "China", "Republic of Moldova": "Moldova", "Republic of France": "France","Great Britain": "England"}
and then pass is to replace function, for more details and parameters check pandas.DataFrame.replace
This method does replace all at once by creating the
dictionary
with all the names of interest, ultimately will standardize yourdataframe
column
based on this dictionary and no need to run individually for eachCountry
.
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