[英]Is there a way to merge duplicated rows and sum values on pandas?
I have the following DataFrame (example):我有以下 DataFrame (示例):
page_name ![]() |
amount_spent![]() |
---|---|
México opina![]() |
50302 ![]() |
De política![]() |
49779 ![]() |
El financiero![]() |
72300 ![]() |
México opina![]() |
32000 ![]() |
De política![]() |
22000 ![]() |
I have been trying to make it look like this with groupby
on Pandas unsuccesfully:我一直试图在 Pandas 上使用
groupby
使其看起来像这样,但未成功:
page_name ![]() |
amount_spent![]() |
---|---|
México opina![]() |
82302 ![]() |
De política![]() |
71779 ![]() |
El financiero![]() |
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
:使用
groupby
和sum
:
df.groupby(['page_name']).sum()
You can use groupby
and reset_index()
:您可以使用
groupby
和reset_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.