user3084006 user3084006 - 1 year ago 98
Python Question

Python Pandas counting and summing specific conditions

Are there single functions in pandas to perform the equivalents of SUMIF, which sums over a specific condition and COUNTIF, which counts values of specific conditions from Excel?

I know that there are many multiple step functions that can be used for

for example for

I can use
( x: condition), or df.size())
then use

and for
I can use
(groupby functions
and look for my answer or use a filter and the

Is there simple one step process to do these functions where you enter the condition and the data frame and you get the sum or counted results?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download