两个由 API 填充的 pandas DataFrame。需要以特定格式加入 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)