이승훈
이승훈

Reputation: 360

pandas multi-column merge on index

hello i'm newbie in pandas

for example, datas of cryptocurrency are as below

BTC

time(index) open high low close value
0           1     4    1    2     1
1           2     5    2    3     2

ETH

time(index) open high low close value
1           1     1    1    1     1

and I want merge these datas as blow

BTC X ETH

                      BTC                        ETH
time(index) open high low close value  open high low close value
0           1     4    1    2     1    NaN  NaN  NaN  NaN   NaN
1           2     5    2    3     2     1    1    1    1     1

is there any way to merge?

Upvotes: 1

Views: 60

Answers (1)

jezrael
jezrael

Reputation: 862591

Use concat with parameter keys for first level of MultiIndex:

df = pd.concat([df1, df2], keys=('BTC','ETH'), axis=1)
print (df)
             BTC                       ETH                      
            open high low close value open high  low close value
time(index)                                                     
0              1    4   1     2     1  NaN  NaN  NaN   NaN   NaN
1              2    5   2     3     2  1.0  1.0  1.0   1.0   1.0

Upvotes: 2

Related Questions