JuniorESE
JuniorESE

Reputation: 267

How to resample multiple dataframes?

I have 27 dataframes with same index and columns. These dataframes are hourly data and I want to convert all of them into monthly data. I went looking for a shortcut. While doing this, I want to sum the data. I've put these dataframes in a list.

df_list =  [
           df1, df2, df3, df4, df5, df6, df7, df8, df9
           df10, df11, df12, df13, df14, df15, df16, df17, df18
           df19, df20, df21, df22, df23, df24, df25, df26, df27
           ]

for df in df_list:
    df = df.resample('M').sum()


Example of any dataframe:

Date                     A     B     C     D
2019-10-01 00:00:00    3.4   2.5   1.6    5.1
2019-10-01 01:00:00    2.3   2.9   4.1    5.9
2019-10-01 02:00:00    1.7   6.7   9.2    4.8
2019-10-01 03:00:00    1.8   1.8   3.6    2.7
2019-10-01 04:00:00    6.1   3.4   2.3    3.1

Output of this code is not my desire output. How can I do that?

Upvotes: 1

Views: 316

Answers (1)

jezrael
jezrael

Reputation: 862406

One idea is use list comprehension and assign back output to list:

df_list = [df.resample('M').sum() for df in df_list]

Or create new list with resampled DataFrames:

dfs = []
for df in df_list:
    df = df.resample('M').sum()
    dfs.append(df)

Another idea is overwrite list by positions with loop by range:

for i in range(len(df_list)):
    df_list[i] = df_list[i].resample('M').sum()

Upvotes: 2

Related Questions