Python 基于列合并 Pandas 中数据帧的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23919563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merge rows of a dataframe in pandas based on a column
提问by user3527975
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
采纳答案by TimmyCarbone
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的评论以重置索引并具有非索引数据框。
回答by 8one6
df = dataframe.groupby('date').sum()