2

I have daily timeseries data that I resampled to weekly data and summed the 'sales' column:

df_weekly = df.resample('W', on='TransxDate').agg({'sales': 'sum'})

How can also get 'count' of sales? If I use

df_weekly = df.resample('W', on='TransxDate').agg({'sales': 'sum', 'sales': 'count'})

I only get the count. If I reverse the order, I only get the sum. I suspect the problem is using the same column name with multiple functions. Is there a way to specify the output column name so that I can perform multiple agg functions on the same column? Or some other solution?

jub
  • 377
  • 1
  • 2
  • 14

1 Answers1

2

Try this:

df.resample('W', on='TransxDate')['sales'].agg(['sum', 'count'])

or you can create a multilevel index with the column aggregating then the functions as level 1. By, using double brackets [[]].

df.resample('2M', on='Date1')[['Number']].agg(['sum', 'count'])
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • 1
    Both work, thanks. And I was able to extend this to multiple columns using `df_weekly = df.resample('W', on='TransxDate')[['sales', 'items']].agg(['sum', 'count'])` Now I just need to read up on how to work with the resulting multi-index(?) dataframe. – jub Jun 27 '21 at 19:25
  • @jub you can flatten the multiIndex using [this post](https://stackoverflow.com/a/43859132/6361531). Or you can select the using tuples with loc. – Scott Boston Jun 27 '21 at 20:55