熊猫计数和求和的具体条件

熊猫中是否有单一的函数来执行相当于 SUMIF(对特定条件求和)和 康蒂夫(从 Excel 中计算特定条件的值)的操作?

我知道有许多多步函数可以用于

例如对于 sumif我可以使用 (df.map(lambda x: condition), or df.size())然后使用 .sum()

对于 countif,我可以使用 (groupby functions和寻找我的答案或使用一个过滤器和 .count())

是否有一个简单的步骤来完成这些函数,你输入条件和数据帧,然后得到总和或计数结果?

344374 次浏览

You didn't mention the fancy indexing capabilities of dataframes, e.g.:

>>> df = pd.DataFrame({"class":[1,1,1,2,2], "value":[1,2,3,4,5]})
>>> df[df["class"]==1].sum()
class    3
value    6
dtype: int64
>>> df[df["class"]==1].sum()["value"]
6
>>> df[df["class"]==1].count()["value"]
3

You could replace df["class"]==1by another condition.

You can first make a conditional selection, and sum up the results of the selection using the sum function.

>> df = pd.DataFrame({'a': [1, 2, 3]})
>> df[df.a > 1].sum()
a    5
dtype: int64

Having more than one condition:

>> df[(df.a > 1) & (df.a < 3)].sum()
a    2
dtype: int64

If you want to do COUNTIF, just replace sum() with count()

I usually use numpy sum over the logical condition column:

>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame({'Age' : [20,24,18,5,78]})
>>> np.sum(df['Age'] > 20)
2

This seems to me slightly shorter than the solution presented above