python - 属性错误: 'list' object has no attribute 'dropna' (outlier)

标签 python dataframe k-means outliers

所以我试图通过创建一个函数来删除异常值:

def remove_outlier_IQR(data):
Q1 = data.quantile(0.25)
Q3 = data.quantile(0.75)
Inter_Q = Q3-Q1
df_final = [~((data<(Q1 - 1.5*Inter_Q)) | (data>(Q3 + 1.5*Inter_Q)))]
return df_final

然后我将异常值删除到带有异常值的指定列上。

df_outlier_removed=remove_outlier_IQR(df[["Umur","Skor Belanja (1-100)"]])
df_outlier_removed.dropna(axis=0, inplace=True)
df_outlier_removed

但是,它返回错误为

AttributeError: 'list' object has no attribute 'dropna'

最佳答案

您有一个列表,并且在整个函数中将其保留为列表。如果您想使用 DataFrame.dropna() 那么这对您有用:

def remove_outlier_IQR(data):
Q1 = data.quantile(0.25)
Q3 = data.quantile(0.75)
Inter_Q = Q3-Q1
list_final = [~((data<(Q1 - 1.5*Inter_Q)) | (data>(Q3 + 1.5*Inter_Q)))]
df_final = pd.DataFrame(list_final)
return df_final

pd.DataFrame() 在退出函数之前将列表转换为 DataFrame。

关于python - 属性错误: 'list' object has no attribute 'dropna' (outlier),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73151888/

相关文章:

python - Django 1.3 或更低版本的 Django Admin 中的自定义过滤器

r - 将数据框摘要保存为数据框

r - 通过为每组选择一行来折叠数据框

python - Python scikit-learn 每次运行后聚类结果的变化

Matlab:Kmeans每次给出不同的结果

python - 如何使用正则表达式在字符串中查找美国邮政编码?

python - Pandas:语言环境格式在 style.format() 中不起作用

python - 使用 matplotlib 在图例中制作自定义垂直线标记

python - 当一个数据帧为空时迭代两个 pandas 数据帧错误

machine-learning - k-means会陷入无限循环吗?