python - 如何从 Pandas 数据框中获取 1 和 0 的最大连续数量
问题描述
我想从每行的熊猫数据框中获取最大数量的连续 1 和 0
import pandas as pd
d=[[0,0,1,0,1,0],[0,0,0,1,1,0],[1,0,1,1,1,1]]
df = pd.DataFrame(data=d)
df
Out[4]:
0 1 2 3 4 5
0 0 0 1 0 1 0
1 0 0 0 1 1 0
2 1 0 1 1 1 1
输出应如下所示:
Out[5]:
0 1 2 3 4 5 Ones Zeros
0 0 0 1 0 1 0 1 2
1 0 0 0 1 1 0 2 3
2 1 0 1 1 1 1 4 1
解决方案
利用boolean masking
witheq
和shift
。我们检查当前值是否等于1
or0
和下一个值是否等于1
or 0
。True
这样我们就可以用&得到数组,False
这样我们就可以sum
结束它们了axis=1
:
m1 = df.eq(0) & df.shift(axis=1).eq(0) # check if current value is 0 and previous value is 0
m2 = df.shift(axis=1).isna() # take into account the first column which doesnt have previous value
m3 = df.eq(1) & df.shift(-1, axis=1).eq(1) # check if current value is 1 and next value is 1
m4 = df.shift(-1, axis=1).isna() # take into account the last column which doesnt have next value
df['Ones'] = (m1 | m2).sum(axis=1)
df['Zeros'] = (m3 | m4).sum(axis=1)
输出
0 1 2 3 4 5 Ones Zeros
0 0 0 1 0 1 0 2 1
1 0 0 0 1 1 0 3 2
2 1 0 1 1 1 1 1 4
推荐阅读
- javascript - 量角器 - 使用他们的孩子识别元素
- helpndoc - 在 HelpnDoc 中向 CHM 项目添加自定义 CSS 内容
- php - 我想在我的协议结束日期中添加月份
- php - laravel注册后如何去掉默认邮件发送
- cross-compiling - 使用 MingW 将 Python 扩展从 Linux 交叉编译到 Win
- php - 使用 MySQL 和约束搜索匹配的标签(相似性搜索)
- java - 当依赖类存在多个 bean 时如何自动装配?
- javascript - Array.map 记录但未显示在屏幕上
- sql-server - 批量插入,无法批量加载。格式文件的未知版本
- java - swagger core 2.0 禁用端点的安全性