Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Aggregating by two different columns

I’m trying to aggregate this using python pandas,
I’m trying to find the Sum of spend and visitors for each network, but only aggregregate them if the months are the same

for example

month network spend visitors
9 CNBC 10 2
10 BBC 10 1
9 BBC 10 2
10 CNBC 10 2
10 CNBC 10 2

should result

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

month network spend visitors
9 CNBC 10 2
9 BBC 10 2
10 CNBC 20 4
10 BBC 10 1

how would I be able to do this?

>Solution :

You can group your pandas dataframe by network and by month and then call the sum method.

df.groupby(['network', 'month']).sum()

Returns:

network     month       spend   visitors
BBC            9           10          2
BBC           10           10          1
CNBC           9           10          2
CNBC          10           20          4 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading