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
| 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