Python/Pandas:一旦满足间隔条件,就使用多个函数变量(列)进行聚合
我有一个带有段、时间戳和不同列的数据帧Python/Pandas:一旦满足间隔条件,就使用多个函数变量(列)进行聚合,python,pandas,conditional-statements,aggregate,Python,Pandas,Conditional Statements,Aggregate,我有一个带有段、时间戳和不同列的数据帧 Segment Timestamp Value1 Value2 Value2_mean 0 2018-11... 180 156 135 0 170 140 135 0 135 1 1 ... 我希望使用“段”聚合/分组此数据
Segment Timestamp Value1 Value2 Value2_mean
0 2018-11... 180 156 135
0 170 140 135
0 135
1
1
...
我希望使用“段”聚合/分组此数据帧,并在满足此间隔条件后立即获取段的第一个时间戳,然后获取此段的时间间隔(以秒为单位)。因为一个函数有更多的值,所以我认为聚合不起作用
value2_mean-std(value2)类似这样的东西?我没有彻底测试它
def calc(grp):
if grp.Value1.sub(grp.Value2_mean).abs().lt(grp.Value2.std()).any():
return grp["Timestamp"].iloc[-1] - grp["Timestamp"].iloc[0]
return np.nan
df.groupby("Segment").apply(calc)
grouped = dataSeg.groupby(['Segment'])
def grouping(df)
a = np.array(df['Value_1'])
b = np.array(df['Value2'])
c = np.array(df['Value2_mean'])
d = np.array(df['Timestamp'])
for x in a:
categories = np.logical_and(
(c-np.std(b)<= x),
(c+np.std(b)>= x))
if np.any(categories):
return d[categories]-d[0]
grouped.apply(grouping)
def calc(grp):
if grp.Value1.sub(grp.Value2_mean).abs().lt(grp.Value2.std()).any():
return grp["Timestamp"].iloc[-1] - grp["Timestamp"].iloc[0]
return np.nan
df.groupby("Segment").apply(calc)