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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.