I have a dataset (Pandas dataframe called df) that looks like:
var1 var2 var3
0 2018-05-02 04:53:46 150785 2018-05-02 04:53:46
1 2018-05-02 06:38:58 150785 2018-05-02 06:38:58
2 2018-05-03 00:35:25 145510 2018-05-03 00:35:25
3 2018-05-03 06:33:53 145510 2018-05-03 06:33:53
4 2018-05-03 06:48:56 145510 2018-05-03 06:48:56
ie I have multiple intraday entries. How can I generate daily averages for this time series?
use pd.Grouper
:
# df['var1'] = pd.to_datetime(df['var1'])
df = df.groupby(pd.Grouper(key='var1', freq='1d'))['var2'].mean().reset_index()
df
output:
var1 var2
0 2018-05-02 150785
1 2018-05-03 145510
You can use Pandas' resample:
df_avg = df.resample('D').mean()
Eg.: Example Input Dataframe print(df)
:
DateTime S1 S2 S3
0 2020-07-15 16:27:01.221660 652.382636 661.053102 387.247039
1 2020-07-15 16:27:01.221660 953.427100 873.895507 397.911431
2 2020-07-16 16:27:01.221660 652.382636 661.053102 387.247039
3 2020-07-16 16:27:01.221660 953.427100 873.895507 397.911431
Code:
df = df.set_index("DateTime")
df["S1"] = df["S1"].astype(float)
df["S2"] = df["S2"].astype(float)
df["S3"] = df["S3"].astype(float)
df_avg = df.resample('D').mean()
Output:
S1 S2 S3
DateTime
2020-07-15 802.904868 767.474304 392.579235
2020-07-16 802.904868 767.474304 392.579235
This gives daily averages of S1, S2 and S3.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.