【问题标题】:Excluding Columns of a pandas dataframe based on row mean values根据行平均值排除熊猫数据框的列
【发布时间】:2018-09-13 14:25:55
【问题描述】:

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

【问题讨论】:

    标签: pandas subset mean


    【解决方案1】:

    您可以将allle 一起使用

    # 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
    

    【讨论】:

    • 看起来很完美 (-:
    • 你能解释一下吗?看起来很漂亮。
    最近更新 更多