python - Pandas :将掩码应用于多索引数据框

标签 python pandas multi-index

我有一个带有 MultiIndex 列的 pandas 数据框,有 3 个级别:

import itertools
import numpy as np

def mklbl(prefix, n):
    return ["%s%s" % (prefix, i) for i in range(n)]


miindex = pd.MultiIndex.from_product([mklbl('A', 4)])

micolumns = pd.MultiIndex.from_tuples(list(itertools.product(['A', 'B'], ['a', 'b', 'c'], ['foo', 'bar'])),
                                      names=['lvl0', 'lvl1', 'lvl2'])

dfmi = pd.DataFrame(np.arange(len(miindex) * len(micolumns)).reshape((len(miindex), len(micolumns))),
                    index=miindex,
                    columns=micolumns).sort_index().sort_index(axis=1)

lvl0   A                       B                    
lvl1   a       b       c       a       b       c    
lvl2 bar foo bar foo bar foo bar foo bar foo bar foo
A0     1   0   3   2   5   4   7   6   9   8  11  10
A1    13  12  15  14  17  16  19  18  21  20  23  22
A2    25  24  27  26  29  28  31  30  33  32  35  34
A3    37  36  39  38  41  40  43  42  45  44  47  46

我想根据另一个具有索引的最后两个级别的数据框来屏蔽此数据框:

cols = micolumns.droplevel(0).unique()
a_mask = pd.DataFrame(np.random.randn(len(dfmi.index), len(cols)), index=dfmi.index, columns=cols)
a_mask = (np.sign(a_mask) > 0).astype(bool)

        a             b             c       
      foo    bar    foo    bar    foo    bar
A0  False  False  False   True   True  False
A1   True  False   True  False   True   True
A2   True   True   True   True  False  False
A3   True  False  False   True   True  False

我想做的是根据 a_mask 屏蔽原始数据帧。 假设当 a_mask 为真时,我想将原始条目设置为零。

我尝试使用 pd.IndexSlice,但它无提示地失败了(即我可以运行以下代码,但没有任何效果:

dfmi.loc[:, pd.IndexSlice[:, a_mask]] = 0  #dfmi is unchanged

有什么建议可以实现吗?

编辑 在我的用例中,标签是用笛卡尔积构造的,因此会有 (lev0、lev1、lev2) 的所有组合。 但情况是 lev0 可以取 2 个值 {A, B},而 lev1 可以取 3 个值 {a, b, c}

最佳答案

我觉得用这种方式比较安全。

dfmi.where(a_mask.loc[:,dfmi.columns.droplevel(0)].values,0)
Out[191]: 
lvl0   A               B            
lvl1   a       b       a       b    
lvl2 bar foo bar foo bar foo bar foo
A0     0   0   0   2   0   0   0   6
A1     9   8  11   0  13  12  15   0
A2     0  16  19  18   0  20  23  22
A3    25   0   0   0  29   0   0   0

关于python - Pandas :将掩码应用于多索引数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47516935/

相关文章:

python - 我正在尝试使用 Python 将文本文件转换为 CSV

python - 检测丢失的时间戳

python - 在新的多索引级别下连接 Pandas 列

python - 从多索引数据框中删除最后一行

python - 参数错误: ValueError: 'params' arg (<class 'list' >) can be only a tuple or a dictionary

python - 如何在特定字符之前拆分字符串?

python - 如何在子图中绘制多个 Seaborn 联合图

Python - 使用 Pandas 和 openpyxl 修改现有的 excel

python - 在 python 中快速访问/查询大的分隔文本文件

python - 如何在 pyomo 模型中提取索引变量信息并构建 Pandas Dataframe