簡體   English   中英

基於其他 dataframe 索引在特定級別上重新索引 Multiindex dataframe

[英]Reindex Multiindex dataframe on a specific level based on other dataframe index

我有一個 Multiindex dataframe [id, currency] 作為輸入。 我希望它被水平貨幣上的price dataframe 指數過濾。 任何貢獻將不勝感激。

balance dataframe:

                balance
id   currency   
1    JPY        2322
     USD        w3r234
     KWN        23423
2    POPO       234234 
     USDC       3w4234
     BTC         54545
     ETH         2323412

price dataframe:

JPY        0.3
KWN        0.5
POPO       0.2 
USDC       0.5
ETH        0.3

所需的 output 是比例balance dataframe:

                balance
id   currency   
1    JPY        2322
     KWN        23423
2    POPO       234234 
     USDC       3w4234
     ETH         2323412

我的代碼:

idx = price.index
balance = balance.reindex(idx, level='currency', fill_value=0).sort_index()

訪問所需級別的值並根據price dataframe 中存在的項目進行過濾:

df = balance[np.in1d(balance.index.get_level_values(1), price.index)]

暫無
暫無

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

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