gpt4 book ai didi

python - 清理数据框中可能存在的错误值

转载 作者:行者123 更新时间:2023-12-01 00:09:06 27 4
gpt4 key购买 nike

我正在处理一个充满一些错误的数据集。

在此数据集中,有很多像 0xFFFF 这样的错误值,我想清除它们。清理此数据集并获得一些连贯值的最有效方法是什么?

我尝试过df = df.apply(lambda x: pd.to_numeric(x,errors = 'coerce'))但还有一个问题,例如,一月有 31 天,二月有 28 天,所以 2 月 29/30/31 的行已经填充了“NaN”。

我的目标是检查一行是否不是数字(例如 0XFFFF),并通过一致的内容更改该值(例如最后 2 个值和下 2 个值的平均值)

清除数据:

    January  February   March  April  May           june  July  August
0 -5 -7.0 -7 2.0 12 17 20 20
1 -6 -6.0 -8 3.0 14 16 20 19
2 -5 -5.0 -5 4.0 15 17 17 19
3 -3 -6.0 -3 6.0 16 14 18 18
4 -6 -8.0 -2 4.0 13 15 17 17
5 -11 -9.0 1 5.0 12 17 17 18
6 -6 5.0 2 4.0 8 17 16 16
7 -8 -11.0 1 6.0 7 15 17 17
8 -11 -12.0 1 7.0 6 14 17 15
9 -8 -9.0 2 7.0 5 Sun 15 14
10 -8 -6.0 1 8.0 5 14 15 14
11 -8 -5.0 3 8.0 6 13 16 15
12 -8 -4.0 5 9.0 8 11 16 15
13 -10 1.0 5 10.0 10 14 13 15
14 -10 3.0 7 8.0 12 15 14 48
15 -9 -9.0 0xFFFF 8.0 13 15 -6 18
16 -6 -6.0 2 9.0 14 15 15 19
17 -6 -6.0 -1 11.0 13 17 15 21
18 -4 -4.0 -2 10.0 15 16 15 24
19 -8 2.0 -1 11.0 15 19 17 21
20 -9 3.0 0 12.0 15 20 18 21
21 -14 1.0 1 9.0 18 19 19 26
22 -15 -3.0 2 7.0 18 20 0xFFFF 17
23 -17 -4.0 3 8.0 16 22 24 18
24 -19 -6.0 4 8.0 15 22 23 17
25 -23 -8.0 3 6.0 16 21 26 17
26 -8 -8.0 4 9.0 16 22 26 16
27 -9 -11.0 5 11.0 17 21 21 17
28 -5 NaN 5 14.0 16 21 22 17
29 -6 NaN 3 16.0 17 17 23 18
30 -7 NaN 3 NaN 17 NaN 21 17

工作后的预期输出(NaN 必须保留,但所有其他必须更改(不是数值)):

     January  February   March  April  May           june  July  August  \
0 -5 -7.0 -7 2.0 12 17 20 20
1 -6 -6.0 -8 3.0 14 16 20 19
2 -5 -5.0 -5 4.0 15 17 17 19
3 -3 -6.0 -3 6.0 16 14 18 18
4 -6 -8.0 -2 4.0 13 15 17 17
5 -11 -9.0 1 5.0 12 17 17 18
6 -6 5.0 2 4.0 8 17 16 16
7 -8 -11.0 1 6.0 7 15 17 17
8 -11 -12.0 1 7.0 6 14 17 15
9 -8 -9.0 2 7.0 5 14 15 14
10 -8 -6.0 1 8.0 5 14 15 14
11 -8 -5.0 3 8.0 6 13 16 15
12 -8 -4.0 5 9.0 8 11 16 15
13 -10 1.0 5 10.0 10 14 13 15
14 -10 3.0 7 8.0 12 15 14 48
15 -9 -9.0 4.5 8.0 13 15 -6 18
16 -6 -6.0 2 9.0 14 15 15 19
17 -6 -6.0 -1 11.0 13 17 15 21
18 -4 -4.0 -2 10.0 15 16 15 24
19 -8 2.0 -1 11.0 15 19 17 21
20 -9 3.0 0 12.0 15 20 18 21
21 -14 1.0 1 9.0 18 19 19 26
22 -15 -3.0 2 7.0 18 20 19 17
23 -17 -4.0 3 8.0 16 22 24 18
24 -19 -6.0 4 8.0 15 22 23 17
25 -23 -8.0 3 6.0 16 21 26 17
26 -8 -8.0 4 9.0 16 22 26 16
27 -9 -11.0 5 11.0 17 21 21 17
28 -5 NaN 5 14.0 16 21 22 17
29 -6 NaN 3 16.0 17 17 23 18
30 -7 NaN 3 NaN 17 NaN 21 17

谢谢!

最佳答案

使用DataFrame.applypd.to_numeric :

df = df.apply(lambda x: pd.to_numeric(x,errors = 'coerce'))

如果您想保留 str 列,您可以选择要清理的列:

cols = [my_list of columns]
df[cols] = df[cols].apply(lambda x: pd.to_numeric(x,errors = 'coerce'))

我们还可以使用DataFrame.stack + DataFrame.unstack

df = pd.to_numeric(df.stack(),errors='coerce').unstack() 

输出

  january   february
0 -1 -1.0
1 -3 -2.0
2 -5 NaN
3 1 1.0
4 0 0.0
5 -6 6.0
6 -7 4.0
7 -5 2.0

更新

df = ( pd.to_numeric(df.stack(),errors='coerce')
.unstack().interpolate().where(df.notna()) )


January February March April May june July August
0 -5.0 -7.0 -7.0 2.0 12.0 17.0 20.0 20.0
1 -6.0 -6.0 -8.0 3.0 14.0 16.0 20.0 19.0
2 -5.0 -5.0 -5.0 4.0 15.0 17.0 17.0 19.0
3 -3.0 -6.0 -3.0 6.0 16.0 14.0 18.0 18.0
4 -6.0 -8.0 -2.0 4.0 13.0 15.0 17.0 17.0
5 -11.0 -9.0 1.0 5.0 12.0 17.0 17.0 18.0
6 -6.0 5.0 2.0 4.0 8.0 17.0 16.0 16.0
7 -8.0 -11.0 1.0 6.0 7.0 15.0 17.0 17.0
8 -11.0 -12.0 1.0 7.0 6.0 14.0 17.0 15.0
9 -8.0 -9.0 2.0 7.0 5.0 14.0 15.0 14.0
10 -8.0 -6.0 1.0 8.0 5.0 14.0 15.0 14.0
11 -8.0 -5.0 3.0 8.0 6.0 13.0 16.0 15.0
12 -8.0 -4.0 5.0 9.0 8.0 11.0 16.0 15.0
13 -10.0 1.0 5.0 10.0 10.0 14.0 13.0 15.0
14 -10.0 3.0 7.0 8.0 12.0 15.0 14.0 48.0
15 -9.0 -9.0 4.5 8.0 13.0 15.0 -6.0 18.0
16 -6.0 -6.0 2.0 9.0 14.0 15.0 15.0 19.0
17 -6.0 -6.0 -1.0 11.0 13.0 17.0 15.0 21.0
18 -4.0 -4.0 -2.0 10.0 15.0 16.0 15.0 24.0
19 -8.0 2.0 -1.0 11.0 15.0 19.0 17.0 21.0
20 -9.0 3.0 0.0 12.0 15.0 20.0 18.0 21.0
21 -14.0 1.0 1.0 9.0 18.0 19.0 19.0 26.0
22 -15.0 -3.0 2.0 7.0 18.0 20.0 21.5 17.0
23 -17.0 -4.0 3.0 8.0 16.0 22.0 24.0 18.0
24 -19.0 -6.0 4.0 8.0 15.0 22.0 23.0 17.0
25 -23.0 -8.0 3.0 6.0 16.0 21.0 26.0 17.0
26 -8.0 -8.0 4.0 9.0 16.0 22.0 26.0 16.0
27 -9.0 -11.0 5.0 11.0 17.0 21.0 21.0 17.0
28 -5.0 NaN 5.0 14.0 16.0 21.0 22.0 17.0
29 -6.0 NaN 3.0 16.0 17.0 17.0 23.0 18.0
30 -7.0 NaN 3.0 NaN 17.0 NaN 21.0 17.0

关于python - 清理数据框中可能存在的错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59753154/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com