python - Filling a pandas dataframe with values from a second dataframe with some rows and columns in common -


let suppose have 2 pandas dataframes df1 , df2

df1          s1   s2   s3     bob  nan  nan  nan     john nan  nan  nan     matt nan  nan  nan 

and

df2          s1   s3   s4     bob  32   11   22     matt 1   nan    2 

i fill df1 values df2 rows , columns exist in df1 output

     s1   s2   s3 bob  32   nan   11 john nan  nan  nan matt 1    nan  nan 

this means, in toy case, i'm not interested in column s4 ofdf2 fill df1. attempt use merge have sadly failed , end dataframe nan.

inplace operation
use pd.dataframe.update
overwrites positions in df1 there non-null value in df2

df1.update(df2)  df1          s1  s2    s3 bob   32.0 nan  11.0 john   nan nan   nan matt   1.0 nan   nan 

produce copy 1
use pd.dataframe.align, pd.dataframe.fillna, , pd.dataframe.reindex_like
fillna won't work unless index , columns aligned.

pd.dataframe.fillna(*df1.align(df2)).reindex_like(df1)          s1  s2    s3 bob   32.0 nan  11.0 john   nan nan   nan matt   1.0 nan   nan 

produce copy 2
pd.dataframe.combine_first , pd.dataframe.reindex_like
it's debatable 1 put first. considering df1 nan doesn't matter. keep pre-existing non-null values in df1. otherwise, switch positions df2.combine_first(df1).

df1.combine_first(df2).reindex_like(df1)          s1  s2    s3 bob   32.0 nan  11.0 john   nan nan   nan matt   1.0 nan   nan 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -