Home > other >  Aggregate 15 min data to 1 hour data based on another column in python?
Aggregate 15 min data to 1 hour data based on another column in python?

Time:07-06

I got the following dataframe as an example using pandas:

Timestamp Computer no. Memory Usage in %
05-07-2022 08:00:00 1 25
05-07-2022 08:00:00 2 56
05-07-2022 08:00:00 3 34
05-07-2022 08:15:00 1 23
05-07-2022 08:15:00 2 63
05-07-2022 08:15:00 3 23
05-07-2022 08:30:00 1 26
05-07-2022 08:30:00 2 56
05-07-2022 08:30:00 3 32
05-07-2022 08:45:00 1 23
05-07-2022 08:45:00 2 15
05-07-2022 08:45:00 3 86
05-07-2022 09:00:00 1 43
05-07-2022 09:00:00 2 26
05-07-2022 09:00:00 3 21

I would like to aggregate the time from a 15 Minute intervall to 1 hour. But I have different Computer No. in the same dataset. Firstly how do I aggregate the time to 1 hour and secondly how do I do it for each computer seperatly?

Thanks!

CodePudding user response:

You can extract the hour value from the timestamp and then groupby -

df['hour'] = pd.to_datetime(df['Timestamp']).dt.hour
df.groupby(['hour', 'Computer no.']).agg('mean').reset_index()

Output

   hour  Computer no.  Memory Usage in %
0     8             1              24.25
1     8             2              47.50
2     8             3              43.75
3     9             1              43.00
4     9             2              26.00

CodePudding user response:

This should work:

df = df.set_index('Timestamp')

df1 = df[df['Computer no.']==1].resample('1H').mean()
df2 = df[df['Computer no.']==2].resample('1H').mean()
df3 = df[df['Computer no.']==3].resample('1H').mean()
  • Related