我有一个熊猫数据框
a b c d e f
0 0.025641 0.554686 0.988809 0.176905 0.050028 0.333333
1 0.027151 0.520914 0.985590 0.409572 0.163980 0.424242
2 0.028788 0.478810 0.970480 0.288557 0.095053 0.939394
3 0.018692 0.450573 0.985910 0.178048 0.118399 0.484848
4 0.023256 0.787253 0.865287 0.217591 0.205670 0.303030
和列列表
cols_list = ['a', 'd', 'f']
我想筛选出所有这些列的值都在第99个百分点以上的所有行。我可以做类似的事情:
for col in cols_list:
df[f'q_{col}'] = df[col].quantile([0.99]).values[0]
for col in cols_list:
df = df[df[col] <= df[f'q_{col}']]
有更有效的方法吗?
您可以使用运算符le
将数据框与分位数进行比较,然后用于all/any
检查行中的值:
valids = df[cols_list].le(df[cols_list].quantile(0.99)).all(1)
df[valids]
输出:
a b c d e f
0 0.025641 0.554686 0.988809 0.176905 0.050028 0.333333
3 0.018692 0.450573 0.985910 0.178048 0.118399 0.484848
4 0.023256 0.787253 0.865287 0.217591 0.205670 0.303030
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句