Pandas Combine dataframes different indices

Viewed 3869

I have two dataframes df_1 and df_2 with different indices and columns. However, there are some indices and columns overlapping.

I created a dataframe df with the union of the indices and of the columns: therefore there are not repeating indices or columns.

I would like to fill the dataframe df in the following way:

for x in df.index:
  for y in df.columns:
    df.loc[x,y] = df_1.loc[x,y] if (x,y) in (df_1.index,df_1.columns) else df_2.loc[x,y]

Can anyone tell me an efficient way to do so?

Thanks!

1 Answers
Related