在Panda Python

时间:2017-08-08 12:14:12

标签: python pandas csv error-handling

我现在有一组数据,你可以在这里看到;

enter image description here

我正在尝试使用Panda中的.std()和.mean()函数来查找偏差并拒绝异常值。不幸的是,我不断收到代码下面显示的错误。我不知道为什么,可能是因为标题不是数字?我不确定。

def reject_outliers(new1, m=3):
        return new1[abs(new1 - np.mean(new1)) < m * np.std(new1)]
new2 = reject_outliers(new1, m=3)
new2.to_csv('final.csv')

ValueError:只能将大小为1的数组转换为Python标量

1 个答案:

答案 0 :(得分:1)

隔离数字列并仅将转换应用于它们

# get list of numeric columns
numcols = list(new1.select_dtypes(include=['number']).columns.values

# run function only on numeric columns
new1[numcols] = reject_outliers(new1[numcols], m=3)