简体   繁体   中英

How can I aggregate (sum, mean, etc.) values and create a new Pandas dataframe based on that?

I have a Pandas dataframe <pandas.core.frame.DataFrame> that has multiple date columns.

Year           Month          Count1    Count2
2015-01-01     2015-05-01     11        23
2015-01-01     2015-03-01     13        24
2020-01-01     2020-05-01     12        22
2020-01-01     2020-05-01     43        13
...

So, it indicates that the second row falls into March in the month category and 2015 in the year category. What I want to do is create a new dataframe that aggregates (let's do sum) the rows that fall into the same category.

For example, if I want to aggregate by year

Month          Count1    Count2
2015-05-01     11        23
2020-01-01     55        35
...

by month, it will be like

Month          Count1    Count2
2015-01-01     24        47
2015-03-01     13        24
2020-05-01     55        35
...

Any help?

This operation can be done by;

agg_col = "Year"

new_df = df.groupby(by=agg_col, as_index=False).agg({"Count1": "sum", "Count2": "sum"})

And you can change agg_col to Month if you want to group by month.

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.

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