def Resample_10mins(df, ZTD_station):
# ensure the time column is in the right format
df['Date'] = pd.to_datetime(df.Date)
# round to the nearest 10 minute interval
# if you want to floor / ceil the time, you may use
#`dt.floor` or `dt.ceil` instead of `dt.round`
df['rounded_to_nearest_10_min'] = df.Date.dt.round('10min')
# to get the mean of all columns
df = df.groupby('rounded_to_nearest_10_min').agg('mean')
# to get the mean of a specific column
df = df.groupby('rounded_to_nearest_10_min').agg({ZTD_station: 'mean'})
# Rename date column
df = df.rename(columns={df.columns[0]: 'Date' })
# df.rename(columns={'rounded_to_nearest_10_min': 'Date'}, inplace=True)
return df
我有下面的代码,我用它来重新采样我的数据帧从30秒到10分钟的速率。但是,我注意到列和行结构发生了变化(比较第二和第三数据帧),我想要第二数据帧的结构而不是第三数据帧。
Date GNSS_BIEL
0 2011-01-01 00:00:00 2.247777
1 2011-01-01 00:00:30 2.246933
2 2011-01-01 00:01:00 2.245638
3 2011-01-01 00:01:30 2.244568
4 2011-01-01 00:02:00 2.243413
Date
rounded_to_nearest_10_min
2011-01-01 00:00:00 2.244251
2011-01-01 00:10:00 2.242808
2011-01-01 00:20:00 2.242657
2011-01-01 00:30:00 2.243564
2011-01-01 00:40:00 2.249966
您可以使用内置的示例方法:
df.resample('10min', on='Date').mean()
详细信息请参见本教程。