I have two dataframes sharing the same indices. I would like to get a third dataframe which contains the data of the first dataframe for the rows which are not null
and the rows from the second dataframe otherwise. I could that if there was an option of rewriting the how
parameter of the pandas.merge
.
Maybe there is a fast way of doing this, which is not obvious at the moment.
EDIT: The indices overlapping in the dataframes are the same, but it might be that some indices appear in one dataframe but not the other. This makes iterating over the indices a costly solution.
So here it is with the data: df_1
and df_2
as inputs and df_3
as the output.
df_1=
0 1 2 3
bar one NaN NaN NaN NaN
baz two AI BI CI DI
foo one AJ BJ CJ DJ
two AK BK CK DK
qux one AL BL CL DL
two AM BM CM DM
other one NaN NaN NaN NaN
df_2=
0 1 2 3
bar one AA BA CA DA
two AB BB CB DB
baz one AC BC CC DC
two AD BD CD DD
foo one AE BE CE DE
two AF BF CF DF
qux two AG BG CG DG
df_3=
0 1 2 3
bar one AA BA CA DA
two AB BB CB DB
baz one AC BC CC DC
two AI BI CI DI
foo one AJ BJ CJ DJ
two AK BK CK DK
qux one AL BL CL DL
two AM BM CM DM
other one NaN NaN NaN None