0

I have a data frame with over 180 columns that I need to sum over a time interval. The below resample code works fine when I enter the column names directly but not in a loop. Because of the large number of columns, I cannot write each of the column names separately.

The code that works:

df_agg = df.resample('1D', on='Date').agg({'col1' : 'sum', 'col2' :'sum'})

The code that does not work:

for x in range(180):
    df_agg = df.iloc[:, 0:x].resample('1D', on='Date').sum()

I get the following error: 'The grouper name Date is not found'

The date column is in DateTime. I tried setting the Date column as the index, but then it says that the resample function only works with DateTime. What should be done now? Any help would be highly appreciated.

Abir
  • 57
  • 5
  • always put FULL error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback. – furas Oct 05 '22 at 22:15
  • maybe first resample all `df` and later run loop to sum columns. Or use `for`-loop to generate dictionary like `agg( {column_name: "sum" for column_name in df.columns} )` – furas Oct 05 '22 at 22:19
  • if you want to sum all columns then maybe you need only `.sum()` with `.agg()` – furas Oct 05 '22 at 22:21
  • frankly, it would be simpler if you would create minimal working code with example data directly in code - so we could copy and test it. – furas Oct 05 '22 at 22:22
  • Thank you for your suggestions. It works for the whole data frame. I will simply have to trim the summed-up version afterward. – Abir Oct 06 '22 at 08:08

0 Answers0