2 回答
TA贡献1825条经验 获得超4个赞
首先获取timestamp
每组GroupBy.transform
,GroupBy.first
然后比较Series.between
:
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['g'] = df['Start'].cumsum()
df1 = df[df['g'].ne(0)].copy()
#
s = df1.groupby('g')['timestamp'].transform('first')
df1 = df1[df1['timestamp'].between(s, s + pd.Timedelta(30, 's'))]
#
df2 = df1.groupby('g').agg(start_time=('timestamp','first'),
end_time=('timestamp','last'),
Average=('data','mean')).reset_index(drop=True)
print (df2)
start_time end_time Average
0 2020-01-15 08:05:10.083 2020-01-15 08:05:10.183 20.25
1 2020-01-15 10:45:35.767 2020-01-15 10:45:46.870 23.75
TA贡献1846条经验 获得超7个赞
试试这个代码。
df['timestamp'] = pd.to_datetime(df['timestamp'])
start_time_list = []
end_time_list = []
average_list = []
for start_ind in df[df['Start'] == 1].index:
end_ind = np.where(df['timestamp'] <= df.iloc[start_ind]['timestamp'] + pd.to_timedelta(30, unit = 's'))[0][-1] + 1
average = df['data'].iloc[start_ind:end_ind].mean()
start_time_list.append(df.iloc[start_ind]['timestamp'])
end_time_list.append(df.iloc[end_ind]['timestamp'])
average_list.append(average)
output = pd.DataFrame({"start_time":start_time_list,
"end_time":end_time_list,
"average":average_list})
添加回答
举报