我有一个包含各种数值变量(如温度、速度等)的数据框,我正试图在其上运行一些代码,如用平均值替换异常值和创建散点图。然而,我不断得到我在标题中引用的错误。。。我不确定我哪里出错了,因为这段代码已经在其他数据帧上工作过了。你知道吗
以下是我的数据帧示例:
import pandas as pd
df = pd.DataFrame({'temp': [.2, naN, .12],
'speed': [1, 1, 0],
'weekday': [1, 2, 3]})
下面是我正在使用的实际代码(步骤1只是导入它,工作正常):
import pandas as pd
cars = pd.read_csv("C:/Users/Downloads/file.csv")
第二步是我开始出现问题的地方:
import numpy as np
outliers = []
outliers.append(cars[['temp', 'speed']])
for j in outliers:
upper_quartile = np.nanpercentile(cars[j], 75)
lower_quartile = np.nanpercentile(cars[j], 25)
iqr = upper_quartile - lower_quartile
upper_whisker = upper_quartile + 1.5*iqr
lower_whisker = np.maximum(lower_quartile - 1.5*iqr, 0)
cars[j] = np.where((cars[j] <= lower_whisker) |
(cars[j] >= upper_whisker), np.nan, cars[j])
这应该是用NaN填充异常值,但是在运行时我得到了布尔数据帧错误消息。运行下一位以列的平均值替换缺少的值时出现相同的错误消息:
for v in outliers:
cars[v].fillna(cars[v].mean(), True)
目前没有回答
相关问题 更多 >
编程相关推荐