gpt4 book ai didi

python-3.x - 如何在列 Pandas 中找到连续零的最大计数?

转载 作者:行者123 更新时间:2023-12-03 17:44:07 24 4
gpt4 key购买 nike

我有数据框,想检查 B 列中连续零值的最大计数。
示例输入和输出:

df = pd.DataFrame({'B':[1,3,4,0,0,11,1,15,0,0,0,87]})

df_out = pd.DataFrame({'max_count':[3]})
这怎么可能?

最佳答案

一种 NumPy 方式 -

a = df['B'].values
m1 = np.r_[False, a==0, False]
idx = np.flatnonzero(m1[:-1] != m1[1:])
out = (idx[1::2]-idx[::2]).max()
循序渐进——
# Input data as array
In [83]: a
Out[83]: array([ 1, 3, 4, 0, 0, 11, 1, 15, 0, 0, 0, 87])

# Mask of starts and ends for each island of 0s
In [193]: m1
Out[193]:
array([False, False, False, False, True, True, False, False, False,
True, True, True, False, False])

# Indices of those starts and ends
In [85]: idx
Out[85]: array([ 3, 5, 8, 11])

# Finally the differencing between starts and ends and max for final o/p
In [86]: out
Out[86]: 3
这可以转换为单行:
np.diff(np.flatnonzero(np.diff(np.r_[0,a==0,0])).reshape(-1,2),axis=1).max()

关于python-3.x - 如何在列 Pandas 中找到连续零的最大计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63737388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com