Reputation: 171
Is there anyway to use pandas to count the number of times that a certain value occurs in each column?
data = pd.DataFrame({'userID':['Luis', 'Mike', 'Harvey'], 'category1':[True, False, True], 'category2': [True, True, False], 'category3':[False, False, False]})
Let's say I want to count the number of 'True' boolean per category to get a return of:
Category 1 -- Category 2 -- Category 3
2 2 0
How would I go about doing this?
Upvotes: 1
Views: 100
Reputation: 863481
You can count occurence by sum
of boolean mask - True
values are processes like 1
s:
#count all columns without first to Series
print (data.iloc[:, 1:].sum().astype(int))
category1 2
category2 2
category3 0
dtype: int32
#or to one row DataFrame
df = data.iloc[:, 1:].sum().astype(int).to_frame().T
General solution with eq
(==
):
val = True
df = data.iloc[:, 1:].eq(val).sum().astype(int).to_frame().T
print (df)
category1 category2 category3
0 2 2 0
Upvotes: 1
Reputation: 316
a = dict(df.category1.value_counts())
This gives you output like {True : 20, False : 15}
Upvotes: 0