我需要为多个列确定满足特定条件的最大数量的连续值。
如果我的 df 是:
A B C D E
26 24 21 23 24
26 23 22 15 23
24 19 17 11 15
27 22 28 24 24
26 27 30 23 11
26 26 29 27 29
我想知道每列出现超过 25 的数字的最大连续次数。所以输出将是:
A 3
B 2
C 3
D 1
E 1
使用下面的代码,我可以一次获得一列的结果;有没有办法像上面那样创建一个表而不是为每一列重复(我总共有超过 40 列)。
df.A.isnull().astype(int).groupby(df.A.notnull().astype(int).cumsum()).sum().max()
提前致谢。
最佳答案
这是你想要的吗? pandas
方法(PS:从没想过我可以把它写成一行 LOL)
(df>25).apply(lambda x :x.groupby(x.diff().ne(0).cumsum()).cumcount()+1).mask(df<25).max()
Out[320]:
A 3.0
B 2.0
C 3.0
D 1.0
E 1.0
dtype: float64
关于python - 如何找到多列的最大连续数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46737323/