pandas :用0替换非数字单元 [英] Pandas: Replacing Non-numeric cells with 0
本文介绍了 pandas :用0替换非数字单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这种格式的Pandas Dataframe
I have the Pandas Dataframe in this format
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
我想用pandas中的0替换所有非数字单元.我正在尝试类似的操作,但无济于事,
I want to replace all non-numeric cells with 0 in pandas. I am trying some thing like this but nothing works,
training_data['usagequantity'].replace({'^([A-Za-z]|[0-9]|_)+$': 0}, regex=True)
任何提示我该怎么做:
推荐答案
您可以使用to_numeric
方法,但它不会改变当前值.您需要将列设置为新值:
You can use the to_numeric
method, but it's not changing the value in place. You need to set the column to the new values:
training_data['usagequantity'] = (
pd.to_numeric(training_data['usagequantity'],
errors='coerce')
.fillna(0)
)
to_numeric 设置非数字值到NaNs
,然后链接的 fillna 方法将NaNs
替换为零.
to_numeric sets the non-numeric values to NaNs
, and then the chained fillna method replaces the NaNs
with zeros.
这篇关于 pandas :用0替换非数字单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文