pd.to_numeric

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 作用:将参数转换为数字类型。

作用

作用:将参数转换为数字类型。

默认返回dtype为float64或int64, 具体取决于提供的数据。使用downcast参数获取其他dtype。

参数描述

参数 描述

args 接受scalar, list, tuple, 1-d array, or Series类型

errors 有3种类型{‘ignore’, ‘raise’, ‘coerce’}, 默认为‘raise’

downcast {‘integer’, ‘signed’, ‘unsigned’, ‘float’} , default None,默认返回float64或int64

注意downcast的意思是向下转换

errors中参数的解释

'raise’参数:无效的解析将引发异常

'corece’参数:将无效解析设置为NaN

‘ignore’参数:**无效的解析将返回输入

downcast中参数的意义

默认的None就是不进行处理

‘integer’和’signed’:最小的有符号整数dtype(最小值np.int8)

‘unsigned’:最小的unsigned int dtype(np.uint8)

‘float’:最小的float dtype(np.float32)

返回值:如果解析成功,则为数字。其中返回类型取决于输入。如果为Series,则为Series,否则为ndarray。

实例

import pandas as pd
import numpy as np
s = pd.Series(['apple', '1.0', '2','2019-01-02',1, False,None,pd.Timestamp('2018-01-05')])
# to_numeric是在object,时间格式中间做转换,然后再使用astype做numeric类型的内部转换
pd.to_numeric(s, errors='raise') # 遇到非数字字符串类型报错,bool类型报错,时间类型转换为int
pd.to_numeric(s, errors='ignore') # 只对数字字符串转换,其他类型一律不转换,包含时间类型
pd.to_numeric(s, errors='coerce')  # 将时间字符串和bool类型转换为数字,其他均转换为NaN
# downcast 可以进一步转化为int或者float
pd.to_numeric(s) # 默认float64类型
pd.to_numeric(s, downcast='signed') # 转换为整型
# astype中的error没有`coerce`选项,所以只适合`numeric`内部类型的转换,比如将int32转换为int64,int32转换为float32
# 而不适合在object,时间格式之间做转换,
s.astype('int32',errors='raise')
s.astype('int32',errors='ignore')  # 对object无效,astype只能对numeric类型生效
目录
相关文章
|
12月前
|
Python
df获取最后一行数据
df获取最后一行数据
391 0
|
22天前
|
机器学习/深度学习 索引 Python
array, list, tensor,Dataframe,Series之间互相转换总结
array, list, tensor,Dataframe,Series之间互相转换总结
|
2月前
|
API 索引 Python
【Pandas】已完美解决:AttributeError: ‘DataFrame‘ object has no attribute ‘ix‘
【Pandas】已完美解决:AttributeError: ‘DataFrame‘ object has no attribute ‘ix‘
99 0
pandas list\dict 转换为DataFrame
pandas list\dict 转换为DataFrame
pandas list\dict 转换为DataFrame
|
11月前
|
Cloud Native Go Python
解决Pandas KeyError: “None of [Index([...])] are in the [columns]“问题
解决Pandas KeyError: “None of [Index([...])] are in the [columns]“问题
318 0
|
12月前
|
Python
dataframe 中float转字符串
要将DataFrame中的浮点数列转换为字符串列,可以使用`astype()`方法将列的数据类型转换为字符串。以下是一个示例: 假设有一个DataFrame `df`,其中包含一个名为`column_name`的浮点数列: ```python import pandas as pd # 示例DataFrame df = pd.DataFrame({'column_name': [1.0, 2.5, 3.2, 4.7]}) # 将浮点数列转换为字符串列 df['column_name'] = df['column_name'].astype(str) # 打印转换后的DataFrame
301 0
|
机器学习/深度学习 数据采集 数据挖掘
Time Series Data
机器学习中的时间序列数据(Time Series Data)是指按时间顺序排列的数据集,其中每个数据点都包含一个或多个特征值。时间序列数据通常用于预测未来事件、
82 0
Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.
Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.
Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.
TypeError: ufunc subtract cannot use operands with types dtype('O') and dtype('M8[ns]')
TypeError: ufunc subtract cannot use operands with types dtype('O') and dtype('M8[ns]')
|
数据库
Data truncation: Out of range value for column ‘estimate_score‘
Data truncation: Out of range value for column ‘estimate_score‘