【发布时间】:2021-01-23 00:11:00
【问题描述】:
我在数据框中有如下所示格式的数据:
metric timestamp cas_pre fl_rat ...
0 2017-04-06 11:25:00 687.982849 1627.040283 ...
1 2017-04-06 11:30:00 693.427673 1506.217285 ...
2 2017-04-06 11:35:00 692.686310 1537.114807 ...
....
45 2017-04-06 11:35:00 51987.427673 1537.114807 ...
....
101003 2017-04-06 11:35:00 692.686310 1537.114807 ...
很明显,第 45 行需要删除,因为它是一个异常。有多列和相当多的行(100,000+)。现在我想从中删除异常值,并且一直在使用代码:
drop_df = df.drop(columns=['timestamp'])
drop_df = drop_df[(np.abs(stats.zscore(drop_df)) < 3).all(axis=1)]
但是,这会给我没有时间戳的数据。这是因为我不能在 z 分数计算中使用时间戳。但是,我想保留时间戳,其相关性在使用 z 分数进行过滤时完全丢失。如下所示:
metric timestamp cas_pre fl_rat ...
0 2017-04-06 11:25:00 687.982849 1627.040283 ...
1 2017-04-06 11:30:00 693.427673 1506.217285 ...
2 2017-04-06 11:35:00 692.686310 1537.114807 ...
....
101003 2017-04-06 11:35:00 692.686310 1537.114807 ...
我怎样才能做到这一点?
【问题讨论】:
标签: python pandas dataframe statistics outliers