gpt4 book ai didi

python - Pandas :用 0 替换非数字单元格

转载 作者:太空宇宙 更新时间:2023-11-03 15:00:47 25 4
gpt4 key购买 nike

我有这种格式的 Pandas Dataframe

0          or LIST requests
1 us-west-2
2 1.125e-05
3 0
4 3.032e-05
5 0
6 7.28e-06
7 or LIST requests
8 3.1e-07
9 0
10 0
11 1.067e-05
12 0.00011983
13 0.1075269
14 or LIST requests
15 us-west-2
16 0
17 2.88e-06
18 ap-northeast-2
19 5.52e-06
20 6.15e-06
21 3.84e-06
22 or LIST requests

我想用 0 替换 pandas 中的所有非数字单元格。我正在尝试这样的事情,但没有任何效果,

training_data['usagequantity'].replace({'^([A-Za-z]|[0-9]|_)+$': 0}, regex=True)

任何提示我该怎么做:

最佳答案

您可以使用 to_numeric 方法,但它不会就地更改值。您需要将列设置为新值:

training_data['usagequantity'] = (
pd.to_numeric(training_data['usagequantity'],
errors='coerce')
.fillna(0)
)

to_numeric将非数字值设置为 NaNs,然后链接 fillna方法用零替换 NaN

关于python - Pandas :用 0 替换非数字单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38133085/

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