Pandas 基于行平均值排除数据帧的列

Pandas 基于行平均值排除数据帧的列,pandas,subset,mean,Pandas,Subset,Mean,我有一个熊猫数据框,df 并计算了行平均值: df['means']=df.mean(axis=1) means col1 col2 col3 2 3 1 2 2 2 1 3 1 1 1 1 1

我有一个熊猫数据框,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

看起来很完美(-:你能解释一下吗?看起来很漂亮。