简体   繁体   English

有没有办法合并 pandas 上的重复行和总和值?

[英]Is there a way to merge duplicated rows and sum values on pandas?

I have the following DataFrame (example):我有以下 DataFrame (示例):

page_name page_name amount_spent金额
México opina墨西哥意见 50302 50302
De política德政 49779 49779
El financiero金融界 72300 72300
México opina墨西哥意见 32000 32000
De política德政 22000 22000

I have been trying to make it look like this with groupby on Pandas unsuccesfully:我一直试图在 Pandas 上使用groupby使其看起来像这样,但未成功:

page_name page_name amount_spent金额
México opina墨西哥意见 82302 82302
De política德政 71779 71779
El financiero金融界 72300 72300

This is that the duplicated rows on page_name merged, and the amount_spent in the merged rows were sum.这是page_name上的重复行合并了,合并行中的amount_spent是sum。

How can I achieve this on Pandas while creating a new DataFrame?在创建新的 DataFrame 时,如何在 Pandas 上实现这一点?

Use groupby and sum :使用groupbysum

df.groupby(['page_name']).sum()

You can use groupby and reset_index() :您可以使用groupbyreset_index()

df_grouped = pd.DataFrame(df.groupby('page_name')['amount_spent'].sum()).reset_index()

which will return a new dataframe as you want.它将根据需要返回一个新的dataframe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM