Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Combine dataframes different indices

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!

like image 795
riccio777 Avatar asked Jul 19 '26 22:07

riccio777


1 Answers

I think you need DataFrame.combine_first:

df_1 = pd.DataFrame({'A':[1,2,3],
                     'E':[4,5,6],
                     'V':[7,8,9],
                     'D':[1,3,5]}, 
                     index=pd.to_datetime(['2017-01-05', '2017-01-04', '2017-01-01']))

print (df_1)
            A  D  E  V
2017-01-05  1  1  4  7
2017-01-04  2  3  5  8
2017-01-01  3  5  6  9

df_2 = pd.DataFrame({'A':[1,2,3],
                     'B':[4,5,6],
                     'C':[7,8,9]}, index=pd.date_range('2017-01-01', periods=3)) * 10

print (df_2)
             A   B   C
2017-01-01  10  40  70
2017-01-02  20  50  80
2017-01-03  30  60  90

df = df_1.combine_first(df_2)
print (df)
               A     B     C    D    E    V
2017-01-01   3.0  40.0  70.0  5.0  6.0  9.0
2017-01-02  20.0  50.0  80.0  NaN  NaN  NaN
2017-01-03  30.0  60.0  90.0  NaN  NaN  NaN
2017-01-04   2.0   NaN   NaN  3.0  5.0  8.0
2017-01-05   1.0   NaN   NaN  1.0  4.0  7.0
like image 52
jezrael Avatar answered Jul 21 '26 10:07

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!