This is my first Multiindex DataFrame df0
Attributes Adj Close Close High
Symbols AARON AART AARON AART AARON AART
Date
2021-12-01 111.3 512.2 111.3 512.2 114.0 519.5
2021-12-02 116.6 512.5 116.6 512.5 116.8 519.9
2021-12-03 117.2 522.3 117.2 522.3 122.5 526.0
Next DataFrame df1
Attributes Adj Close Close High
Symbols FINP FLEX FINP FLEX FINP FLEX
Date
2021-12-01 204.4 18.0 204.4 18.0 213.6 18.0
2021-12-02 204.5 18.7 204.5 18.7 206.3 18.9
2021-12-03 200.8 19.5 200.8 19.5 205.8 19.6
Now I want to get final dataFrame df as
Attributes Adj Close Close High
Symbols AARON AART FINP FLEX AARON AART FINP FLEX AARON AART FINP FLEX
Date
2021-12-01 111.3 512.2 204.4 18.0 111.3 512.2 204.4 18.0 114.0 519.5 213.6 18.0
2021-12-02 116.6 512.5 204.5 18.7 116.6 512.5 204.5 18.7 116.8 519.9 206.3 18.9
2021-12-03 117.2 522.3 200.8 19.5 117.2 522.3 200.8 19.5 122.5 526.0 205.8 19.6
Which function/method should I use to get desired output
Note: Here added columns information for both dataFrames
df0.columns
MultiIndex([('Adj Close', 'AARON'),
('Adj Close', 'AART'),
( 'Close', 'AARON'),
( 'Close', 'AART'),
( 'High', 'AARON'),
( 'High', 'AART')],
names=['Attributes', 'Symbols'])
df1.columns
MultiIndex([('Adj Close', 'AARON'),
('Adj Close', 'AART'),
( 'Close', 'AARON'),
( 'Close', 'AART'),
( 'High', 'AARON'),
( 'High', 'AART')],
names=['Attributes', 'Symbols'])
>Solution :
You can simply pd.concat them, and use groupby with level=0 (to group by the 0th (1st) level of the index) + first:
df = pd.concat([df0, df1]).groupby(level=0).first()
Output:
>>> df
Adj Close Close High
AARON AART FINP FLEX AARON AART FINP FLEX AARON AART FINP FLEX
Symbols
2021-12-01 111.3 512.2 204.4 18.0 111.3 512.2 204.4 18.0 114.0 519.5 213.6 18.0
2021-12-02 116.6 512.5 204.5 18.7 116.6 512.5 204.5 18.7 116.8 519.9 206.3 18.9
2021-12-03 117.2 522.3 200.8 19.5 117.2 522.3 200.8 19.5 122.5 526.0 205.8 19.6