[英]How can i clean my dataset from outliers as it includes numerical and categorical variables in Python?
I would like to clean my dataset from outliers but just in three specific columns, as the other 10 contain categorical variables. 我想从离群值中清除数据集,但只在三个特定的列中清除,因为其他10个列包含分类变量。 So how can I get my data cleaned by only referring to these specific columns?
那么,如何仅通过引用这些特定列来清理数据呢?
I'd like to use iqr range method. 我想使用iqr range方法。 That's the code i run so far:
那是我到目前为止运行的代码:
import numpy as np
def outliers(x):
return np.abs(x- x.median()) > 1.5*(x.quantile(.75)-x.quantile(0.25))
ath2.Age[outliers(ath2.Age)]
ath2.Height[outliers(ath2.Height)]
ath2.Weight[outliers(ath2.Weight)]
After checking the number of outliers in the columns I'm interested in, I don't know how to proceed further. 在检查了我感兴趣的列中的异常值之后,我不知道如何进一步进行。
If you want the code to be dynamic, you can 1st check the columns which are not categorical by below code: 如果您希望代码是动态的,则可以通过以下代码首先检查未归类的列:
cols = df.columns
num_cols = df._get_numeric_data().columns
##num_cols will contains list of column names which are numeric
## In your case, it should come Age,Height etc.
Alternatively, you can also use include
or exclude
parameters using df.select_dtypes
according to your dataframe 另外,您还可以根据数据
df.select_dtypes
使用df.select_dtypes
来使用include
或exclude
参数
After this run below code from columns from above: 在此之后,从上方的列中运行以下代码:
df[np.abs(df.Data-df.Data.mean()) <= (3*df.Data.std())]
## Df is the dataframe and Data is the name of the column.
#In your case, it will be Age,Height etc.
OR 要么
If you want to make a new df with only the numerical columns and find out the outliers in one shot, below is the code: 如果要仅用数字列创建新的df并一次性找出离群值,则代码如下:
df[df.apply(lambda x: np.abs(x - x.mean()) / x.std() < 3).all(axis=1)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.