【发布时间】:2021-12-14 20:12:56
【问题描述】:
我有一个带有二进制列 target, 的数据框 df,我想计算 至少 n 连续行的出现次数,使得 df[target] == 1。
我找到了很多关于计算 (确切) n 在某些数据框列上满足给定条件的连续行的出现的答案。但他们并没有解决我的问题。
我可以利用目标是有限的这一事实来构建以下算法来解决我的问题:
target = [0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,1,1,1]
df = pd.DataFrame(
{"target" : target}
)
n = 3
groups = []
for i in range(df.size):
if df["target"].iloc[i] == 0:
continue
group_index_min = df.index[i]
for j in range(i, df.size):
if df["target"].iloc[j] == 1:
group_index_max = df.index[j]
else:
break
current_group = (group_index_min, group_index_max)
is_sub_group = False
for group in groups:
a, b = group
if a <= group_index_min and group_index_max <= b:
is_sub_group = True
if (not is_sub_group) and (group_index_max - group_index_min + 1 >= n):
groups.append(current_group)
groups
# >> [(2, 4), (9, 11), (31, 36)]
但是,我更喜欢使用 numpy 或 pandas 的解决方案,更 Pythonic。
有人可以帮助我吗?非常感谢!
【问题讨论】:
标签: python-3.x pandas dataframe