yjasrc
yjasrc

Reputation: 513

Dataframe append with multiindex

I have a dataframe d1 with multiindex of col1 and col2:

              col3   col4   col5
col1   col2  
  1      2      3      4      5
  2      3      4      5      6

And another dataframe d2 with exact same structure:

              col3   col4   col5
col1   col2  
 20     30     40     50     60
  2      3     44     55     66

How to do d1.append(d2), to make it become, which override the previous keys:

              col3   col4   col5
col1   col2  
  1      2      3      4      5
 20     30     40     50     60
  2      3     44     55     66

Upvotes: 0

Views: 52

Answers (2)

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10960

You could use pandas.concat with keep last

pd.concat([df1, df2]).groupby(level=[0, 1]).last()

@BENY's answer is more user friendly and readable.

Upvotes: 1

BENY
BENY

Reputation: 323226

Try with combine_first

out = d2.combine_first(d1)

Upvotes: 3

Related Questions