我有一个熊猫数据框df,并计算了行平均值:
df['means']=df.mean(axis=1)
means col1 col2 col3
2 3 1 2
2 2 1 3
1 1 1 1
1 0 1 2
2 0 1 5
我需要的是一种排除所有值均低于或等于行平均值的所有列的方法。例如,在上面的col2中,所有值都小于或等于平均值,因此应排除在外。因此,输出应为:
means col1 col3
2 3 2
2 2 3
2 1 1
2 1 2
2 0 5
你可以用all
与le
# notice I did not assign the new column means here.
df.loc[:,~df.le(df.mean(1),0).all()]
Out[27]:
col1 col3
0 3 2
1 2 3
2 1 1
3 0 2
4 0 5
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句