我有一個帶有一些離群值的pandas系列。下面是一些模擬資料:
df = pd. DataFrame({'col1': [1200, 400, 50, 75, 8, 9, 8, 7, 6, 5, 4, 6, 6, 8, 3, 6, 6, 7, 6]})
我想用平均值來替代離群值,即>=3個標準差的值。
uj5u.com熱心網友回復:
讓我們來做:
thrs = df['col1'].mean() 3 * df['col1'].std()
df.loc[df['col1'] >= thrs, 'col1'] = df['col1'] .mean()
uj5u.com熱心網友回復:
std_dev = df["col1"].std()。
mean = df["col1"].mean()
df["col1"] = np.where(df.col1 >=3*std_dev, mean, df.col1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/307660.html
標籤:
