Ahmed Gamal
Ahmed Gamal

Reputation: 1706

Filter , group by and count in pandas?

A TSV file contains some user event data :

user_uid category event_type
"11"      "like"   "post"
"33"      "share"  "status"
"11"      "like"   "post"
"42"      "share"  "post"

what is the best way to get the number of post events for each category and for each user_id?

we should show the following output:

user_uid category count
"11"     "like"    2
"42"     "share"   1

Upvotes: 2

Views: 1355

Answers (1)

ALollz
ALollz

Reputation: 59549

Clean up any trailing whitespace so that things group properly. Filter your DataFrame, and then apply groupby + size

df['category'] = df.category.str.strip()
df['user_uid'] = df.user_uid.str.strip()
df[df.event_type == 'post'].groupby(['user_uid', 'category']).size()

Output:

user_uid  category
11        like        2
42        share       1
dtype: int64

Upvotes: 5

Related Questions