我有数据框:
df:
customer sample1 sample2 sample3 sample4
costprice1 10 21 32 43
costprice2 12 24 15 18
costprice3 1 2 15 8
costprice4 16 30 44 58
costprice5 18 33 48 63
costprice6 20 36 52 68
costprice7 22 39 56 73
costprice8 24 42 60 78
costprice9 26 45 64 83
costprice10 28 48 68 88
我想在多于2列中删除值小于15的行
所以这将被丢弃
costprice3 1 2 15 8
在R中我们可以做
df[rowSums(df < 15) <=2 , , drop = FALSE]
可以在大熊猫中完成吗,我曾经使用大熊猫any来过滤掉行
df_fitered = df[(df > threshold).any(1)]
In [16]: df[df.select_dtypes(['number']).lt(15).sum(axis=1) < 3]
Out[16]:
customer sample1 sample2 sample3 sample4
0 costprice1 10 21 32 43
1 costprice2 12 24 15 18
3 costprice4 16 30 44 58
4 costprice5 18 33 48 63
5 costprice6 20 36 52 68
6 costprice7 22 39 56 73
7 costprice8 24 42 60 78
8 costprice9 26 45 64 83
9 costprice10 28 48 68 88
奖励答案:
mask = <condition1>
df[mask & (df.select_dtypes(['number']).lt(15).sum(axis=1) < 3)]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句