0

Trying to change values in column 1 to the values found in column 2 if the values in column 1 == 'nan'.

for row in df.column_1:
    if row == 'nan':
       df.column_1 == df.column_2

But no changes are seen.

Desired Before and After: [sorry, graphic won't upload at the moment]

Brian
  • 2,163
  • 1
  • 14
  • 26
spacedustpi
  • 351
  • 5
  • 18

1 Answers1

1

1st 'nan' is not NaN, if we want to use fillna we need replace

df.column_1=df.column_1.replace('nan',np.nan).fillna(df.column_2) 

or we just using np.where

df.column_1=np.where(df.column_1=='nan',df.column_2,df.column_1) 
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Thanks Wen, the second solution (np.where) worked. The first one (.replace) did not. – spacedustpi Aug 07 '18 at 15:07
  • Taking out the curly brackets allows the first solution to work for my issue. I tried to take out the curlies in an edit, but it said I need to change at least 6 characters to make an edit. Strange. – spacedustpi Aug 07 '18 at 15:20
  • 1
    @spacedustpi yep , I miss type the dataframe replace to series sorry about that – BENY Aug 07 '18 at 15:21