I would like to relocate columns by condition. In case country is 'Japan', I need to relocate last_name and first_name reverse.
df = pd.DataFrame([['France','Kylian', 'Mbappe'],
['Japan','Hiroyuki', 'Tajima'],
['Japan','Shiji', 'Kagawa'],
['England','Harry', 'Kane'],
['Japan','Yuya', 'Ohsako'],
['Portuguese','Cristiano', 'Ronaldo']],
columns=['country', 'first_name', 'last_name'])
Current output is
country first_name last_name
0 France Kylian Mbappe
1 Japan Hiroyuki Tajima
2 Japan Shiji kagawa
3 England Harry Kane
4 Japan Yuya Ohsako
5 Portuguese Cristiano Ronaldo
I would like to make it following.
country first_name last_name
0 France Kylian Mbappe
1 Japan Tajima Hiroyuki
2 Japan Kagawa Shinji
3 England Harry Kane
4 Japan Ohsako Yuya
5 Portuguese Cristiano Ronaldo
Any idea?
Pandas – Replace Values in Column based on Condition. To replace values in column based on condition in a Pandas DataFrame, you can use DataFrame.loc property, or numpy.where (), or DataFrame.where ().
Software Tutorials You can use the following custom function to swap the position of two columns in a pandas DataFrame: def swap_columns(df, col1, col2): col_list = list(df.columns) x, y = col_list.index(col1), col_list.index(col2) col_list [y], col_list [x] = col_list [x], col_list [y] df = df [col_list] return df
Pandas’ loc creates a boolean mask, based on a condition. Sometimes, that condition can just be selecting rows and columns, but it can also be used to filter dataframes. These filtered dataframes can then have values applied to them.
When passing a list of columns, Pandas will return a DataFrame containing part of the data. The result will be similar. In this case, we’ll just show the columns which name matches a specific expression.
Use loc
and swap the "first_name" and "last_name" values for rows whose "country" matches "Japan".
m = df['country'] == 'Japan'
df.loc[m, ['first_name', 'last_name']] = (
df.loc[m, ['last_name', 'first_name']].values)
df
country first_name last_name
0 France Kylian Mbappe
1 Japan Tajima Hiroyuki
2 Japan Kagawa Shiji
3 England Harry Kane
4 Japan Ohsako Yuya
5 Portuguese Cristiano Ronaldo
Another option using rename
and update
:
mp = {'first_name': 'last_name', 'last_name': 'first_name'}
df.update(df.loc[m].rename(mp, axis=1))
df
country first_name last_name
0 France Kylian Mbappe
1 Japan Tajima Hiroyuki
2 Japan Kagawa Shiji
3 England Harry Kane
4 Japan Ohsako Yuya
5 Portuguese Cristiano Ronaldo
### check below
df['first_name'],df['last_name']=np.where(df['country']=='Japan',(df['last_name'],df['first_name']),(df['first_name'],df['last_name']))
country first_name last_name
0 France Kylian Mbappe
1 Japan Tajima Hiroyuki
2 Japan Kagawa Shiji
3 England Harry Kane
4 Japan Ohsako Yuya
5 Portuguese Cristiano Ronaldo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With