创建多索引列数据框

Creating a multi-indexed column dataframe

两个 pandas 数据帧由 API 填充。需要加入特定格式的DataFrame。

当前状态 - 两个数据帧,每个数据帧由时间戳索引

eth_df:
                  close          symbol
timestamp
1541376000000     206.814430     eth
1541462400000     209.108877     eth


btc_df:
                  close          symbol
timestamp
1541376000000     6351.06194     btc
1541462400000     6415.443409    btc

期望状态 - 按时间戳索引和按符号多索引列

portfolio_df:
                  eth            btc
                  close          close
timestamp
1541376000000     206.814430     6351.06194
1541462400000     209.108877     6415.443409

编辑 1: 来自社区的请求:请将 eth_df.to_dict() 和 btc_df.to_dict() 的结果添加到问题中好吗?

这是两者的代码和结果:

btc = cg.get_coin_market_chart_range_by_id('bitcoin','usd', start_date, end_date)

    portfolio_df = pd.DataFrame(data=btc['prices'], columns=['timestamp','close'])
    portfolio_df['symbol'] = 'btc'
    portfolio_df = portfolio_df.set_index('timestamp')
    portfolio_df.to_dict()
{'close': {1541376000000: 6351.061941056285,
  1541462400000: 6415.443408541094,
  1541548800000: 6474.847290336688,

show more (open the raw output data in a text editor) ...

  1627344000000: 'btc',
  1627430400000: 'btc',
  1627516800000: 'btc',
  1627603200000: 'btc',
  1627689600000: 'btc',
  ...}}



eth = cg.get_coin_market_chart_range_by_id('ethereum','usd', start_date, end_date)
eth_df = pd.DataFrame(data=eth['prices'], columns=['timestamp','close'])
eth_df['symbol'] = 'eth'
eth_df = eth_df.set_index('timestamp')
eth_df.to_dict()

{'close': {1541376000000: 206.8144295995958,
  1541462400000: 209.10887661978714,
  1541548800000: 219.16088708430863,

show more (open the raw output data in a text editor) ...

  1627344000000: 'eth',
  1627430400000: 'eth',
  1627516800000: 'eth',
...}}


btc = cg.get_coin_market_chart_range_by_id('bitcoin','usd', start_date, end_date)

我对 CoinGeckoAPI 不是很熟悉,所以假设你得到如下所示的数据框,你没有先设置索引:

from pycoingecko import CoinGeckoAPI
from datetime import datetime
cg = CoinGeckoAPI()

start_date, end_date = 1497484800,1499138400

btc = cg.get_coin_market_chart_range_by_id('bitcoin','usd', start_date, end_date)
btc_df = pd.DataFrame(data=btc['prices'], columns=['timestamp','close'])
btc_df['symbol'] = 'btc'

eth = cg.get_coin_market_chart_range_by_id('ethereum','usd', start_date, end_date)
eth_df = pd.DataFrame(data=eth['prices'], columns=['timestamp','close'])
eth_df['symbol'] = 'eth'

您连接数据帧并进行数据透视:

portfolio_df = pd.concat([btc_df,eth_df]).pivot_table(index="timestamp",columns="symbol")

然后交换级别:

portfolio_df = portfolio_df.swaplevel(axis=1)
portfolio_df

      symbol    btc     eth
                close   close
timestamp       
1497484800000   2444.493712 346.369070
1497571200000   2513.810348 358.284517
1497657600000   2683.571344 372.357011
1497744000000   2577.219361 359.438712
1497830400000   2620.136451 362.044289