import numpy as np
# load data etc
...
def get_count_max_0(df):
"""
Computes the max length of a sequence of zeroes
broken by ones.
"""
values = np.array(df['value'].tolist())
# compute change points where 0 -> 1
cps_1 = np.where(
(values[1:] != values[:-1]) &
(values[1:] == 1)
)[0]
# compute change points where 1 -> 0
cps_0 = np.where(
(values[1:] != values[:-1]) &
(values[1:] == 0)
)[0]
# find lengths of zero chains
deltas = cps_1 - cps_0
# get index of max length
idx = np.where(deltas == deltas.max())[0][0]
# return max length
return deltas[idx]
# group by ID, apply get_count_max_0 to each group and
# convert resulting series back to data frame to match your expected output.
max_counts = df.groupby("ID").apply(get_count_max_0).to_frame("count_max_0")
print(max_counts)
你可以利用
要为每行指定一个组号:
^{pr2}$现在可以按
^{3}$ID
和group
编号分组,以获得所需的值计数:收益率
这应该是有效的:
输出为:
^{pr2}$相关问题 更多 >
编程相关推荐