Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap column values based on a condition in pandas

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?

like image 763
tajihiro Avatar asked Jun 03 '19 05:06

tajihiro


People also ask

How to replace values in column based on condition in pandas?

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 ().

How do I swap two columns in a Dataframe in Python?

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

How do you filter DataFrames in pandas?

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.

What happens when you pass a list of columns to pandas?

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.


2 Answers

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 
like image 165
cs95 Avatar answered Oct 22 '22 15:10

cs95


### 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']))

output:

   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
like image 6
vrana95 Avatar answered Oct 22 '22 16:10

vrana95