Reputation: 1706
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
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