Home > Net >  Aggregate overtimestamps in pandas e calculate mean
Aggregate overtimestamps in pandas e calculate mean

Time:05-24

I have a dataframe structured as well:

Timestamp                      Value
2021-06-07T03:19:49.000 0000   8
2021-06-07T03:20:19.000 0000   4
2021-06-07T03:20:49.000 0000   3
2021-06-08T03:11:05.000 0000   2
2021-06-08T03:11:35.000 0000   6

The result I want is this, where I aggregate per day and compute the mean:

Timestamp    Value
2021-06-07   5
2021-06-08   4

How can I do it using pandas? Do I need to cast the timastamp?

CodePudding user response:

Try Series.Groupby

out = df.groupby(df.Timestamp.dt.date)['Value'].mean().reset_index()
out
Out[82]: 
    Timestamp  Value
0  2021-06-07    5.0
1  2021-06-08    4.0

CodePudding user response:

Make sure the index is a pd.DatetimeIndex, then you can do this:

df = df.resample('D').mean()
  • Related