I have the following DataFrame (example):
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:
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.
How can I achieve this on Pandas while creating a new DataFrame?
Use groupby
and sum
:
df.groupby(['page_name']).sum()
You can use groupby
and reset_index()
:
df_grouped = pd.DataFrame(df.groupby('page_name')['amount_spent'].sum()).reset_index()
which will return a new dataframe
as you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.