tyleha
tyleha

Reputation: 3419

Pandas: Merge two dataframe columns

Consider two dataframes:

df_a = pd.DataFrame([
        ['a', 1],
        ['b', 2],
        ['c', NaN],
    ], columns=['name', 'value'])

df_b = pd.DataFrame([
        ['a', 1],
        ['b', NaN],
        ['c', 3],
        ['d', 4]
    ], columns=['name', 'value'])

So looking like

# df_a
  name  value
0   a   1
1   b   2
2   c   NaN

# df_b
  name  value
0   a   1
1   b   NaN
2   c   3
3   d   4

I want to merge these two dataframes and fill in the NaN values of the value column with the existing values in the other column. In other words, I want out:

# DESIRED RESULT
  name  value
0   a   1
1   b   2
2   c   3
3   d   4

Sure, I can do this with a custom .map or .apply, but I want a solution that uses merge or the like, not writing a custom merge function. How can this be done?

Upvotes: 2

Views: 672

Answers (1)

jezrael
jezrael

Reputation: 862511

I think you can use combine_first:

print (df_b.combine_first(df_a))
  name  value
0    a    1.0
1    b    2.0
2    c    3.0
3    d    4.0

Or fillna:

print (df_b.fillna(df_a))
  name  value
0    a    1.0
1    b    2.0
2    c    3.0
3    d    4.0

Solution with update is not so common as combine_first:

df_b.update(df_a)
print (df_b)
  name  value
0    a    1.0
1    b    2.0
2    c    3.0
3    d    4.0

Upvotes: 3

Related Questions