Home > Back-end >  How to make a new column based on nan conditional?
How to make a new column based on nan conditional?

Time:08-10

I have 2 dataframes, where I was looking if cells of column player in df1 existed in column last_name of df2. I merged on column player and if it is present in df2 it prints the cell but if its not it prints nan(which is what I wanted). I next wanted to make a description column but only for the non nan values. How can I add description for all the values that aren't nan?

df3 = df.merge(df2, left_on = 'player', right_on = 'last_name', how = 'left')

df1

player team position
Tatum Celtics SF
Brown Celtics SG
Smart Celtics PG
Horford Celtics C
Brogdon Celtics PG
Gallinari Celtics F

df2

last_name team position
Durant Nets SF
James Lakers SF
Smart Celtics PG
Horford Celtics C
Davis Lakers C
Curry Warriors PG

I changed the matched_player column name for readability with:

df3.rename(columns = {'last_name':'matched_player'}, inplace=True)

output(df3)

player team position matched_player
Tatum Celtics SF nan
Brown Celtics SG nan
Smart Celtics PG Smart
Horford Celtics C Horford
Brogdon Celtics PG nan
Gallinari Celtics F nan

expected output

player team position matched_player description
Tatum Celtics SF nan
Brown Celtics SG nan
Smart Celtics PG Smart a player from df1
Horford Celtics C Horford a player from df1
Brogdon Celtics PG nan
Gallinari Celtics F nan

CodePudding user response:

You can try np.where

df3['description'] = np.where(df3['matched_player'].notna(), 'a player from df1', '')
# or
df3['description'] = np.where(df3['matched_player'].isna(), '', 'a player from df1')
  • Related