Pandas counting and summing specific conditions
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 5dtype: int64
Having more than one condition:
>> df[(df.a > 1) & (df.a < 3)].sum()a 2dtype: int64
If you want to do COUNTIF
, just replace sum()
with count()
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 3value 6dtype: int64>>> df[df["class"]==1].sum()["value"]6>>> df[df["class"]==1].count()["value"]3
You could replace df["class"]==1
by another condition.