1

I have two pandas, the first one has 2000 rows looks like:

    key            B           C           D
key1              11          nan         nan 
key2              nan         33          88
key3              nan         44          99
key4              22          55          nan 

and the second has about 700 values

    key            B           C           D
key4              22           55         77
key2              55           42         63
key3              53           23         nan 

I wanna iterate my first data frame by keys and replace the nan values by the matching values if exist in the second pandas like this

    key            B           C           D
key1              11          nan         nan 
key2              55          33          88
key3              53          44          99
key4              22          55          77

my two data frames contain strings I used numbers just for the sake of examples

piRSquared
  • 285,575
  • 57
  • 475
  • 624

1 Answers1

1

Assuming those are actual null values...
You want pd.DataFrame.fillna

df1.fillna(df2)

         B     C     D
key                   
key1  11.0   NaN   NaN
key2  55.0  33.0  88.0
key3  53.0  44.0  99.0
key4  22.0  55.0  77.0
piRSquared
  • 285,575
  • 57
  • 475
  • 624