簡體   English   中英

創建多索引列數據框

[英]Creating a multi-indexed column dataframe

由 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)

我對 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM