bismo
bismo

Reputation: 1439

How to merge two pandas dataframes on index but fill missing values

I have two dataframes

df

  x
0 1
1 1
2 1
3 1
4 1

df1

  y
1 1
3 1

And I want to merge them on the index, but still keep the indexes that aren't present in df1. This is my desired output

  x  y
0 1  0
1 1  1
2 1  0
3 1  1
4 1  0

I have tried merging on index, like this

pd.merge(df, df1s, left_index=True, right_index=True)

But that gets rid of the index values not in df1. For example:

  x y
1 1 1
3 1 1

This is not what I want. I have tried both outer and inner join, to no avail. I have also tried reading through other pandas merge questions, but can't seem to figure out my specific case here. Apologies if the merge questions are redundant, but again, I cannot figure out how to merge the way I would like in this certain scenario. Thanks!

Upvotes: 1

Views: 7810

Answers (2)

Paul Mundt
Paul Mundt

Reputation: 509

No need for any complicated merging, you can just copy the column over directly, fill the NaNs, and set the dtype. You can either do this directly, or with pd.concat():

pd.concat([df1, df2], axis=1).fillna(0).astype(int)

   x  y
0  1  0
1  1  1
2  1  0
3  1  1
4  1  0

Upvotes: 0

wwnde
wwnde

Reputation: 26676

Try to concatenate on rows and fill NaNs with 0

pd.concat([df,df1], axis=1).fillna(0)



  x    y
0  1  0.0
1  1  1.0
2  1  0.0
3  1  1.0
4  1  0.0

Upvotes: 3

Related Questions