Trụ sở chính:
Văn phòng: Số 27-3RD, Sunrise D, The Manor Central Park, đường Nguyễn Xiển, phường Đại Kim, quận Hoàng Mai, TP. Hà Nội.
Liên hệ truyền thông: 0929.536.185
Email: [email protected]
Chịu trách nhiệm nội dung: Ông Trần Anh Tú
TEK4.VN giữ bản quyền nội dung trên website này. Cấm sao chép dưới mọi hình thức nếu không có sự chấp thuận bằng văn bản.
The initial dataset.
print(df.head()) Col0 Col1 Col2 Col3 Col4 User_id 0 49 31 93 53 39 44 1 69 13 84 58 24 47 2 41 71 2 43 58 64 3 35 56 69 55 36 67 4 64 24 12 18 99 67First removing the User_id column
filt_df = df.loc[:, df.columns != 'User_id']Then, computing percentiles.
low = .05 high = .95 quant_df = filt_df.quantile([low, high]) print(quant_df) Col0 Col1 Col2 Col3 Col4 0.05 2.00 3.00 6.9 3.95 4.00 0.95 95.05 89.05 93.0 94.00 97.05Next filtering values based on computed percentiles. To do that I use an apply by columns and that's it !
filt_df = filt_df.apply(lambda x: x[(x>quant_df.loc[low,x.name]) & (x < quant_df.loc[high,x.name])], axis=0)Bringing the User_id back.
filt_df = pd.concat([df.loc[:,'User_id'], filt_df], axis=1)Last, rows with NaN values can be dropped simply like this.
filt_df.dropna(inplace=True) print(filt_df.head()) User_id Col0 Col1 Col2 Col3 Col4 1 47 69 13 84 58 24 3 67 35 56 69 55 36 5 9 95 79 44 45 69 6 83 69 41 66 87 6 9 87 50 54 39 53 40