Python DataFrame: replace or combine selected values into main DataFrame

154 Views Asked by At

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!

2

There are 2 best solutions below

3
On BEST ANSWER
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
5
On

Assuming your columns and indices are setup properly, you can just say.

df.loc[df_result.index,df_result.columns] = df_result 

example of it working:

import pandas as pd
import numpy as np
df=pd.DataFrame(data=[ [1 for y in range(5)] for i in range(5)] , columns=list(range(5)))
df.loc[0::2,2]=np.nan
print(df)
df2 = pd.DataFrame(data=[ [2 for y in range(3)] for i in range(2)] , columns=list(range(2,5)),index=range(1,3))
df2.loc[:,3] = np.nan
print(df2)
df.loc[df2.index,df2.columns] = df2
print(df)