我正在研究如何计算符合某些条件的特定列的数量,并过滤任何高于1的列.我认为这将需要添加一个额外的列,包括count(),然后单独的步骤查看count()是否大于1.最后,删除不必要的count()列.

对于下面的例子,我只对大于0的colB、colC、colD和colE感兴趣.

我try 过的代码示例如下:

#Step a
filtData['countCol'] = filtData[(filtData['colB']>0) & (filtData['colC']>0) &
        (filtData['colD']>0) & (filtData['colE']>0)].count()
#Step b
filtData['countCol'] = filtData[filtData['countCol'] > 1]

#Step c
filtData = filtData.drop(columns=['countCol'])

输入:

    colA    colB    colC    colD    colE    colF
0   1105    0.00    867     3.4     0.00    text1
1   1106    3       3.22    1       3       text2
2   1107    0.5     0       0       1       text3
3   1110    0       23      0       0       text4
4   1019    9       0.0     2       0       text5
5   1267    0       0.0     0       2       text6

输出步骤a:

    colA    colB    colC    colD    colE    colF    countCol
0   1105    0.00    867     3.4     0.00    text1   2
1   1106    3       3.22    1       3       text2   4
2   1107    0.5     0       0       1       text3   2
3   1110    0       23      0       0       text4   1
4   1019    9       0.0     2       0       text5   2
5   1267    0       0.0     0       2       text6   1

输出步骤b:

    colA    colB    colC    colD    colE    colF    countCol
0   1105    0.00    867     3.4     0.00    text1   2
1   1106    3       3.22    1       3       text2   4
2   1107    0.5     0       0       1       text3   2
4   1019    9       0.0     2       0       text5   2

输出步骤c:

    colA    colB    colC    colD    colE    colF
0   1105    0.00    867     3.4     0.00    text1
1   1106    3       3.22    1       3       text2
2   1107    0.5     0       0       1       text3
4   1019    9       0.0     2       0       text5

如果有一种方法可以在一个步骤中执行这一操作,并且是优雅的(不是一个表达式太高级而无法理解),那将是理想的.我还在学习pandas,所以执行我正在寻找的过滤可能需要在三个子步骤中被打破.

推荐答案

你可以按列表过滤列,comapre按0和计数Trues按sum,最后过滤行大于1/boolean indexing:

out = filtData[(filtData[['colB','colC','colD','colE']]>0).sum(axis=1) > 1]

或者可以用DataFrame.gt代替greater:

out = filtData[filtData[['colB','colC','colD','colE']].gt(0).sum(axis=1).gt(1)]

print (out)
   colA  colB    colC  colD  colE   colF
0  1105   0.0  867.00   3.4   0.0  text1
1  1106   3.0    3.22   1.0   3.0  text2
2  1107   0.5    0.00   0.0   1.0  text3
4  1019   9.0    0.00   2.0   0.0  text5

How it working:

print (filtData[['colB','colC','colD','colE']].gt(0))
    colB   colC   colD   colE
0  False   True   True  False
1   True   True   True   True
2   True  False  False   True
3  False   True  False  False
4   True  False   True  False
5  False  False  False   True

print (filtData[['colB','colC','colD','colE']].gt(0).sum(axis=1))
0    2
1    4
2    2
3    1
4    2
5    1
dtype: int64

print (filtData[['colB','colC','colD','colE']].gt(0).sum(axis=1).gt(1))
0     True
1     True
2     True
3    False
4     True
5    False
dtype: bool

Python相关问答推荐

将HTML输出转换为表格中的问题

Pandas实际上如何对基于自定义的索引(integer和非integer)执行索引

rame中不兼容的d类型

如何让程序打印新段落上的每一行?

当从Docker的--env-file参数读取Python中的环境变量时,每个\n都会添加一个\'.如何没有额外的?

driver. find_element无法通过class_name找到元素'""

当我try 在django中更新模型时,模型表单数据不可见

使用特定值作为引用替换数据框行上的值

Python—压缩叶 map html作为邮箱附件并通过sendgrid发送

在用于Python的Bokeh包中设置按钮的样式

mdates定位器在图表中显示不存在的时间间隔

freq = inject在pandas中做了什么?''它与freq = D有什么不同?''

如何获得3D点的平移和旋转,给定的点已经旋转?

如何使用Azure Function将xlsb转换为xlsx?

数据框,如果值在范围内,则获取范围和

仅使用预先计算的排序获取排序元素

我什么时候应该使用帆布和标签?

如何防止html代码出现在quarto gfm报告中的pandas表之上

PYTHON中的selenium不会打开 chromium URL

在MongoDB文档中仅返回数组字段