pythonpandasdataframereplacecombiners

Python DataFrame: replace or combine selected values into main DataFrame


I have two pandas DataFrame as below. It contains strings and np.nan values. df =

   A    B    C    D    E    F
0  aaa  abx  fwe  dcs  NaN  gsx
1  bbb  daf  dxs  fsx  NaN  ewe
2  ccc  NaN  NaN  NaN  NaN  dfw
3  ddd  NaN  NaN  asc  NaN  NaN
4  eee  NaN  NaN  cse  NaN  NaN
5  fff  NaN  NaN  wer  xer  NaN

df_result =

   A    C    E    F
2  sfa  NaN  NaN  wes  
4  web  NaN  NaN  NaN  
5  NaN  wwc  wew  NaN  

What I want is copy entire df_result DataFrame to df DataFrame with corresponding columns and index. so my output would be =

   A    B    C    D    E    F
0  aaa  abx  fwe  dcs  NaN  gsx
1  bbb  dxs  fsx  fsx  NaN  ewe
2  sfa  NaN  NaN  NaN  NaN  wes
3  wen  NaN  NaN  asc  NaN  NaN
4  web  NaN  NaN  cse  NaN  NaN
5  NaN  NaN  wwc  wer  wew  NaN

So basically I want to copy exact values of df_result to df even thought ther are np.nan values like A:5 (changed from fff to NaN). Also, I need to keep the order of columns as it is. Please let me know efficient way to do this. Thank you!


Solution

  • df.update(dfr.fillna('NaN'))
    df.replace('NaN',np.nan)
    Out[501]: 
         A    B    C    D    E    F
    0  aaa  abx  fwe  dcs  NaN  gsx
    1  bbb  daf  dxs  fsx  NaN  ewe
    2  sfa  NaN  NaN  NaN  NaN  wes
    3  ddd  NaN  NaN  asc  NaN  NaN
    4  web  NaN  NaN  cse  NaN  NaN
    5  NaN  NaN  wwc  wer  wew  NaN