基于列合并pandas中的数据帧行

use*_*975 7 python pandas

我是熊猫新手.我有一个看起来像这样的数据框

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
Run Code Online (Sandbox Code Playgroud)

如何根据日期合并行并总结同一日期的计数.喜欢在sql中

select sitename, name, date count(*) from table group by date
Run Code Online (Sandbox Code Playgroud)

Tim*_*one 8

如果您想在数据框中保留您的网站名称和名称,您可以执行以下操作:

df = dataframe.groupby(['date', 'sitename', 'name']).sum()
Run Code Online (Sandbox Code Playgroud)

编辑:请参阅@DSM的注释以重置索引并具有非索引数据帧.

  • @user3527975:不,不是。您可能将索引(在本例中是多索引)与列混淆了。您可以添加 `.reset_index()`,或者将 `as_index=False` 添加到 `groupby` 中,例如 `groupby(["date", "sitename", "name"], as_index=False).sum()`。 (3认同)