[英]Pandas dataframe: merge rows into 1 row and sum a coulmn
I have a pandas dataframe that contains user id and ad click (if any) by this user across several days我有一个 pandas dataframe 包含该用户在几天内的用户 ID 和广告点击(如果有)
df =pd.DataFrame([['A',0], ['A',1], ['A',0], ['B',0], ['B',0], ['B',0], ['B',1], ['B',1], ['B',1]],columns=['user_id', 'click_count'])
Out[8]:
user_id click_count
0 A 0
1 A 1
2 A 0
3 B 0
4 B 0
5 B 0
6 B 1
7 B 1
8 B 1
I would like to convert this dataframe into A dataframe WITH 1 row per user where 'click_cnt' now is sum of all click_count across all rows for each user in the original dataframe ie我想将此 dataframe 转换为 dataframe 每个用户有 1 行,其中“click_cnt”现在是原始 Z6A8064B5CCDF479455557055 中每个用户的所有行的所有 click_count 的总和
Out[18]:
user_id click_cnt
0 A 1
1 B 3
What you're after is the function groupby
:你所追求的是 function groupby
:
df = df.groupby('user_id', as_index=False).sum()
Adding the flag as_index=False
will add the keys as a separate column instead of using them for the new index.添加标志as_index=False
会将键添加为单独的列,而不是将它们用于新索引。
groupby
is super useful - have a read through the documentation for more info. groupby
非常有用 - 阅读文档以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.