pandas 如何重新索引多索引数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53286882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:09:28  来源:igfitidea点击:

How to reindex a MultiIndex dataframe

pandasdataframemulti-index

提问by BradB

Is there a way to reindex two dataframes (of differing levels) so that they share a common index across all levels?

有没有办法重新索引两个数据帧(不同级别),以便它们在所有级别共享一个公共索引?

Demo:

演示:

Create a basic Dataframe named 'A':

创建一个名为“A”的基本数据框:

index = np.array(['AUD','BRL','CAD','EUR','INR'])
data = np.random.randint(1, 20, (5,5))
A = pd.DataFrame(data=data, index=index, columns=index)  

Create a MultiIndex Dataframe named 'B':

创建一个名为“B”的多索引数据帧:

np.random.seed(42)
midx1 = pd.MultiIndex.from_product([['Bank_1', 'Bank_2'], 
['AUD','CAD','EUR']], names=['Bank', 'Curency'])
B = pd.DataFrame(np.random.randint(10,25,6), midx1)
B.columns = ['Notional']

Basic DF:

基本DF:

>>> Dataframe A:

        AUD     BRL     CAD     EUR     INR
AUD     7       19      11      11      4
BRL     8       3       2       12      6
CAD     2       1       12      12      17
EUR     10      16      15      15      19
INR     12      3       5       19      7

MultiIndex DF:

多索引DF:

>>> Dataframe B:

                    Notional
Bank    Curency     
Bank_1  AUD         16
        CAD         13
        EUR         22
Bank_2  AUD         24
        CAD         20
        EUR         17

The goal is to:

目标是:

1) reindex B so that its currency level includes each currency in A's index. B would then look like this (see BRL and INR included, their Notional values are not important):

1) 重新索引 B,使其货币水平包括 A 指数中的每种货币。B 看起来像这样(见 BRL 和 INR,它们的名义值并不重要):

                    Notional
Bank    Curency     
Bank_1  AUD         16
        CAD         13
        EUR         22
        BRL         0
        INR         0
Bank_2  AUD         24
        CAD         20
        EUR         17
        BRL         0
        INR         0

2) reindex A so that it includes each Bank from the first level of B's index. A would then look like this:

2) 重新索引 A,使其包括 B 索引的第一级中的每个银行。A 看起来像这样:

               AUD      BRL     CAD     EUR     INR
Bank_1  AUD     7       19      11      11      4
        BRL     8       3       2       12      6
        CAD     2       1       12      12      17
        EUR     10      16      15      15      19
        INR     12      3       5       19      7
Bank_2  AUD     7       19      11      11      4
        BRL     8       3       2       12      6
        CAD     2       1       12      12      17
        EUR     10      16      15      15      19
        INR     12      3       5       19      7

The application of this will be on much larger dataframes so I need a pythonic way to do this.

这个的应用将在更大的数据帧上,所以我需要一种pythonic的方式来做到这一点。

For context, ultimately I want to multiply A and B. I am trying to reindex to get matching indices as that was shown as a clean way to multiply dataframes of various index levels here: Pandas multiply dataframes with multiindex and overlapping index levels

对于上下文,最终我想将 A 和 B 相乘。我正在尝试重新索引以获得匹配的索引,因为这被显示为一种将各种索引级别的数据帧相乘的干净方法: Pandas 将数据帧与多索引和重叠索引级别相乘

Thank you for any help.

感谢您的任何帮助。

回答by YOBEN_S

To get the B using reindex

得到 B 使用 reindex

B.reindex( pd.MultiIndex.from_product([B.index.levels[0], 
A.index], names=['Bank', 'Curency']),fill_value=0)

Out[62]: 
                Notional
Bank   Curency          
Bank_1 AUD            16
       BRL             0
       CAD            13
       EUR            22
       INR             0
Bank_2 AUD            24
       BRL             0
       CAD            20
       EUR            17
       INR             0

To get the A using concat

得到 A 使用 concat

pd.concat([A]*2,keys=B.index.levels[0])
Out[69]: 
            AUD  BRL  CAD  EUR  INR
Bank                               
Bank_1 AUD   10    5   10   14    1
       BRL   17    1   14   10    8
       CAD    3    7    3   15    2
       EUR   17    1   15    2   16
       INR    7   15    6    7    4
Bank_2 AUD   10    5   10   14    1
       BRL   17    1   14   10    8
       CAD    3    7    3   15    2
       EUR   17    1   15    2   16
       INR    7   15    6    7    4