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!
joinwhen doing aleft mergeonindex: You canjoinand fill nas with zero:df.join(df1).fillna(0)howparameter :pd.merge(df, df1, left_index=True, right_index=True, how="left").fillna(0)