[英]Merge rows of a dataframe in pandas based on a column
I am new to pandas. 我是熊猫新手。 I have a dataframe that looks like this 我有一个看起来像这样的数据框
sitename name date count
0 chess.com Autobiographer 2012-05-01 2
1 chess.com Autobiographer 2012-05-05 1
2 chess.com Autobiographer 2012-05-15 1
3 chess.com Autobiographer 2012-05-01 1
4 chess.com Autobiographer 2012-05-15 1
5 chess.com Autobiographer 2012-05-01 1
How to merge the rows based on date and sum up the count for the same date. 如何根据日期合并行并总结同一日期的计数。 Like in sql 喜欢在sql中
select sitename, name, date count(*) from table group by date
If you want to keep your sitename and name in your dataframe, you can do : 如果您想在数据框中保留您的网站名称和名称,您可以执行以下操作:
df = dataframe.groupby(['date', 'sitename', 'name']).sum()
EDIT : See @DSM 's comment to reset the indexes and have a non indexed dataframe. 编辑:请参阅@DSM的注释以重置索引并具有非索引数据帧。
df = dataframe.groupby('date').sum()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.