3

I have 2 Multindex dataframes (date and ticker are indexes)

df_usdtbtc:

                         close
date          ticker                                                     
2017-12-31    USDT_BTC   13769
2018-01-01    USDT_BTC   13351

and df_ethbtc:

                         close
date          ticker                                                     
2017-12-31    USDT_ETH   736
2018-01-01    USDT_ETH   754

Is there any way to merge, concat or join these 2 dataframes to get as a result this dataframe :

                         close
date          ticker                                                     
2017-12-31    USDT_BTC   13769
              USDT_ETH   736
2018-01-01    USDT_BTC   13351
              USDT_ETH   754

To help set up the dataframes :

df_usdtbtc = {'dates':  [dtm(2018, 1, 1),dtm(2018, 1, 2)], 'ticker': ['USDT_BTC', 'USDT_BTC'],'close':[13769,13351]}
df_usdteth = {'dates':  [dtm(2018, 1, 1),dtm(2018, 1, 2)], 'ticker': ['USDT_ETH', 'USDT_ETH'],'close':[736,754]}
df_usdtbtc = pd.DataFrame(data=df_usdtbtc)
df_usdtbtc=df_usdtbtc.set_index(['dates','ticker'])
df_usdteth = pd.DataFrame(data=df_usdteth)
df_usdteth=df_usdteth.set_index(['dates','ticker'])

1 Answer 1

1

Use concat or DataFrame.append with sort_index:

df = pd.concat([df_usdtbtc, df_ethbtc]).sort_index()

Or:

df = df_usdtbtc.append(df_ethbtc).sort_index()

df = pd.concat([df_usdtbtc, df_ethbtc]).sort_index()
print (df)
                     close
date       ticker         
2017-12-31 USDT_BTC  13769
           USDT_ETH    736
2018-01-01 USDT_BTC  13351
           USDT_ETH    754
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.