Home > front end >  Merging dataframes in pandas while ignoring NaN values
Merging dataframes in pandas while ignoring NaN values

Time:06-25

Suppose I have two dataframes:

df_a
     A    B    C
0  1.0  NaN  NaN
1  NaN  1.0  NaN
2  NaN  NaN  1.0

df_b
     A    B    C
0  NaN  NaN  2.0
1  NaN  2.0  NaN
2  2.0  NaN  NaN

How would I go about merging/concatenating them so the result dataframe looks like this:

df_c
     A    B    C
0  1.0  NaN  2.0
1  NaN  2.0  NaN
2  2.0  NaN  1.0

The way I got closer conceptually was by using pd.merge(df_a, df_b, "right"), but all values on df_a ended up replaced.

Is there any way to ignore NaN values when merging?

CodePudding user response:

In your case do combine_first

df_c = df_b.combine_first(df_a)
df_c
Out[151]: 
     A    B    C
0  1.0  NaN  2.0
1  NaN  2.0  NaN
2  2.0  NaN  1.0

CodePudding user response:

df_c = df_a.combine_first(df_b)
df_c
A   B   C
0   1.0 NaN 2.0
1   NaN 1.0 NaN
2   2.0 NaN 1.0

df_d = df_b.combine_first(df_a)
df_d
A   B   C
0   1.0 NaN 2.0
1   NaN 2.0 NaN
2   2.0 NaN 1.0
  • Related