它用于将参数转换为数值类型
这个函数尤其适用于在数据清洗过程中,将数据集中的字符串或其他类型的列转换为数值型数据。
pandas.to_numeric(
arg,
errors='raise',
downcast=None,
dtype_backend=_NoDefault.no_default)
arg | 要转换的数据。可以是标量、列表、元组、一维数组或 Pandas Series |
errors | 决定了当转换过程中遇到无效数据时的行为。
|
downcast | 用于指定转换后的数据类型。
|
import pandas as pd
s = pd.Series(['1.0', '2', -3])
pd.to_numeric(s)
'''
0 1.0
1 2.0
2 -3.0
dtype: float64
'''
pd.to_numeric(s, downcast='integer')
'''
0 1
1 2
2 -3
dtype: int8
'''
s = pd.Series(['apple', '1.0', '2', -3])
pd.to_numeric(s, errors='ignore')
'''
0 apple
1 1.0
2 2
3 -3
dtype: object
'''
pd.to_numeric(s, errors='coerce')
'''
0 NaN
1 1.0
2 2.0
3 -3.0
dtype: float64
'''
pd.to_numeric(s, errors='raise')
'''
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
File D:\conda\lib\site-packages\pandas\_libs\lib.pyx:2369, in pandas._libs.lib.maybe_convert_numeric()
ValueError: Unable to parse string "apple"
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
Cell In[40], line 1
----> 1 pd.to_numeric(s, errors='raise')
File D:\conda\lib\site-packages\pandas\core\tools\numeric.py:185, in to_numeric(arg, errors, downcast)
183 coerce_numeric = errors not in ("ignore", "raise")
184 try:
--> 185 values, _ = lib.maybe_convert_numeric(
186 values, set(), coerce_numeric=coerce_numeric
187 )
188 except (ValueError, TypeError):
189 if errors == "raise":
File D:\conda\lib\site-packages\pandas\_libs\lib.pyx:2411, in pandas._libs.lib.maybe_convert_numeric()
ValueError: Unable to parse string "apple" at position 0
'''