简体   繁体   中英

Appending two dataframes with multiindex

I have two dataframes, each with a multiindex. The multiindex levels share names, but are in a different order. When I append or concat, I would expect pandas to line up the indices just like it aligns index-less columns before appending. Is there a function or an argument I can pass to append or concat to get this to work in the way I desire (and that I think ought to be standard)?

import pandas as pd

df1 = pd.DataFrame(data = {'Name':['Bob','Ann','Sally'], 'Acct':['Savings','Savings','Checking'], 'Value':[101,102,103]})
df1 = df1.set_index(['Name','Acct'])
print(df1)

df2 = pd.DataFrame(data = {'Acct':['Savings','Savings','Checking'], 'Name':['Bob','Ann','Sally'], 'Value':[201,202,203]})
df2 = df2.set_index(['Acct','Name'])
print(df2)

print(df1.append(df2))
print(pd.concat([df1,df2]))


               Value
Name  Acct
Bob   Savings     101
Ann   Savings     102
Sally Checking    103

                Value
Acct     Name
Savings  Bob      201
         Ann      202
Checking Sally    203

                   Value
Name     Acct
Bob      Savings     101
Ann      Savings     102
Sally    Checking    103
Savings  Bob         201
         Ann         202
Checking Sally       203

                   Value
Name     Acct
Bob      Savings     101
Ann      Savings     102
Sally    Checking    103
Savings  Bob         201
         Ann         202
Checking Sally       203

As you can see, after appending or concatenating, my combined index appears to show that, for example, "Sally" is an account, not a name. I'm aware that if I put the index levels in the same order when setting index, I'll get what I want, and that I could reset the index on the frames to align them, but I'm hoping there's a more intuitive way to get the indices to align on name, not on position.

Somewhat of a work around, you can reset_index on both data sets, concat them, then set_index :

print(pd.concat([
    df1.reset_index(),
    df2.reset_index()
], sort=False).set_index([
    'Name',
    'Acct'
]))

                Value
Name  Acct           
Bob   Savings     101
Ann   Savings     102
Sally Checking    103
Bob   Savings     201
Ann   Savings     202
Sally Checking    203

Though I'm not sure why you would want to have multiple rows with the same index...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM