从数据框中排除列,根据其平均值不使用循环 [英] Excluding columns from a dataframe based on their mean without using a loop

查看:134
本文介绍了从数据框中排除列,根据其平均值不使用循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大熊猫数据框df,我想删除一个平均值大于10且小于2的列。如果没有循环,我该怎么做?



我试过这个没有循环

  df = df.drop(df.mean(axis = 1)> 10和df.mean(axis = 1)< 2)


解决方案

你不能使用drop,但是你可以索引...你还需要使用& 而不是

  m = df.mean(axis = 1)#calculate once 
df = df.loc [ ,(m> 10)& (m <2)]


I have a pandas dataframe df and I would like to drop columns which have a mean greater than 10 and less than 2. How can I do it without a loop?

I tried this without a loop

df=df.drop(df.mean(axis=1)>10 and df.mean(axis=1)<2)

解决方案

You can't use drop, but you can index... You also need to use & rather than and:

m = df.mean(axis=1)  # calculate once
df = df.loc[:, (m>10) & (m<2)]

这篇关于从数据框中排除列,根据其平均值不使用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆