pandas - 使用多索引 dataframe,根据另一列的条件获取布尔列的求和结果

我们有一个多索引 dataframe,如下所示:

date   condition_1    condition_2
item1   0    2021-06-10 06:30:00+00:00          True          False
        1    2021-06-10 07:00:00+00:00         False           True
        2    2021-06-10 07:30:00+00:00          True           True
item2   3    2021-06-10 06:30:00+00:00          True          False
        4    2021-06-10 07:00:00+00:00          True           True
        5    2021-06-10 07:30:00+00:00          True           True
item3   6    2021-06-10 06:30:00+00:00          True           True
        7    2021-06-10 07:00:00+00:00         False           True
        8    2021-06-10 07:30:00+00:00          True           True

date 的 value 在项目之间重复(因为 df 是 dataframes 字典上默认连接的结果)。

我们基本上想要向量化的逻辑是“对于所有条件为 true 的每个 date :在所有项目的新结果列中求和条件 2 为 true 的出现次数”。

根据上面的示例,结果基本上看起来像这样(关于它是如何派生的评论:在结果列旁边):

date   condition_1    condition_2    result
item1   0    2021-06-10 06:30:00+00:00          True          False         1 [because condition_1 is True for all items and condition_2 is True once]
        1    2021-06-10 07:00:00+00:00         False           True         0 [condition_1 is not True for all items so condition_2 is irrelevant]
        2    2021-06-10 07:30:00+00:00          True           True         3 [both conditions are True for all 3 items]
item2   3    2021-06-10 06:30:00+00:00          True          False         1 [a repeat for the same reasons]
        4    2021-06-10 07:00:00+00:00          True           True         0 [a repeat for the same reasons]
        5    2021-06-10 07:30:00+00:00          True           True         3 [a repeat for the same reasons]
item3   6    2021-06-10 06:30:00+00:00          True           True         1 [a repeat for the same reasons]
        7    2021-06-10 07:00:00+00:00         False           True         0 [a repeat for the same reasons]
        8    2021-06-10 07:30:00+00:00          True           True         3 [a repeat for the same reasons]

回答1

这是我想出的。

def cond_sum(s):
    return s.cond1.all() * s.cond2.sum()

df.reset_index(level=0, inplace=True)
df['result'] = df.groupby('date').apply(cond_sum)
df.set_index('item', append=True)

然后,如果您想要原始索引,可以将其添加回来。

df.set_index('item', append=True).swaplevel()

请注意,您提到了矢量化,因此您可以将其换成:

dfg = df.groupby(level=0).agg({'cond1': 'all', 'cond2': 'sum'})
df['result'] = dfg.cond1 * dfg.cond2

相似文章

随机推荐

最新文章