pandas.to_numeric转化数据为数字型

简介:
to_numeric(arg, errors='raise', downcast=None)
    Convert argument to a numeric type.
    
    Parameters
    ----------
    arg : list, tuple, 1-d array, or Series
    errors : {'ignore', 'raise', 'coerce'}, default 'raise'
        - If 'raise', then invalid parsing will raise an exception
        - If 'coerce', then invalid parsing will be set as NaN
        - If 'ignore', then invalid parsing will return the input
    downcast : {'integer', 'signed', 'unsigned', 'float'} , default None
        If not None, and if the data has been successfully cast to a
        numerical dtype (or if the data was numeric to begin with),
        downcast that resulting data to the smallest numerical dtype
        possible according to the following rules:
    
        - 'integer' or 'signed': smallest signed int dtype (min.: np.int8)
        - 'unsigned': smallest unsigned int dtype (min.: np.uint8)
        - 'float': smallest float dtype (min.: np.float32)
    
        As this behaviour is separate from the core conversion to
        numeric values, any errors raised during the downcasting
        will be surfaced regardless of the value of the 'errors' input.
    
        In addition, downcasting will only occur if the size
        of the resulting data's dtype is strictly larger than
        the dtype it is to be cast to, so if none of the dtypes
        checked satisfy that specification, no downcasting will be
        performed on the data.
    
        .. versionadded:: 0.19.0
    
    Returns
    -------
    ret : numeric if parsing succeeded.
        Return type depends on input.  Series if Series, otherwise ndarray
    
    Examples
    --------
    Take separate series and convert to numeric, coercing when told to
    
    >>> 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='float')
    0    1.0
    1    2.0
    2   -3.0
    dtype: float32
    >>> pd.to_numeric(s, downcast='signed')
    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
    
    See also
    --------
    pandas.DataFrame.astype : Cast argument to a specified dtype.
    pandas.to_datetime : Convert argument to datetime.
    pandas.to_timedelta : Convert argument to timedelta.
    numpy.ndarray.astype : Cast a numpy array to a specified type.
目录
相关文章
|
1月前
|
Python
如何使用Python的Pandas库进行数据透视图(melt/cast)操作?
Pandas的`melt()`和`pivot()`函数用于数据透视。基本步骤:导入pandas,创建DataFrame,然后使用这两个函数变换数据。示例代码:导入pandas,定义一个包含'Name'和'Age'列的DataFrame,使用`melt()`转为长格式,再用`pivot()`恢复为宽格式。
57 1
|
1月前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名
【4月更文挑战第22天】Pandas Python库提供数据排序和排名功能。使用`sort_values()`按列进行升序或降序排序,如`df.sort_values(by='A', ascending=False)`。`rank()`函数用于计算排名,如`df['A'].rank(ascending=False)`。多列操作可传入列名列表,如`df.sort_values(by=['A', 'B'], ascending=[True, False])`和分别对'A'、'B'列排名。
37 2
|
1月前
|
存储 Python
使用Pandas库对非数值型数据进行排序和排名
在Pandas中,支持对非数值型数据排序和排名。可按以下方法操作:1) 字符串排序,使用`sort_values()`,如`sorted_df = df.sort_values(by='Name', ascending=False)`进行降序排序;2) 日期排序,先用`to_datetime()`转换,再排序,如`sorted_df = df.sort_values(by='Date')`;3) 自定义排序,结合`argsort()`和自定义规则。
42 2
|
1月前
|
索引 Python
如何使用Python的Pandas库进行数据合并和拼接?
Pandas的`merge()`函数用于数据合并,如示例所示,根据'key'列对两个DataFrame执行内连接。`concat()`函数用于数据拼接,沿轴0(行)拼接两个DataFrame,并忽略原索引。
57 2
|
1月前
|
数据挖掘 索引 Python
如何在Python中,Pandas库实现对数据的时间序列分析?
【4月更文挑战第21天】Pandas在Python中提供了丰富的时间序列分析功能,如创建时间序列`pd.date_range()`,转换为DataFrame,设置时间索引`set_index()`,重采样`resample()`(示例:按月`'M'`和季度`'Q'`),移动窗口计算`rolling()`(如3个月移动平均)以及季节性调整`seasonal_decompose()`。这些工具适用于各种时间序列数据分析任务。
31 2
|
1月前
|
存储 JSON 数据处理
从JSON数据到Pandas DataFrame:如何解析出所需字段
从JSON数据到Pandas DataFrame:如何解析出所需字段
68 1
|
1月前
|
数据采集 监控 数据可视化
Pandas平滑法时序数据
【5月更文挑战第17天】本文介绍了使用Python的Pandas库实现指数平滑法进行时间序列预测分析。指数平滑法是一种加权移动平均预测方法,通过历史数据的加权平均值预测未来趋势。文章首先阐述了指数平滑法的基本原理,包括简单指数平滑的计算公式。接着,展示了如何用Pandas读取时间序列数据并实现指数平滑,提供了示例代码。此外,文中还讨论了指数平滑法在实际项目中的应用,如销售预测和库存管理,并提到了在`statsmodels`库中使用`SimpleExpSmoothing`函数进行模型拟合和预测。最后,文章强调了模型调优、异常值处理、季节性调整以及部署和监控的重要性,旨在帮助读者理解和应用这一方法
30 2
 Pandas平滑法时序数据
|
25天前
|
存储 数据采集 JSON
Pandas数据读取三连“坑”
大家小时候有没有用玩儿过一种飞行棋,两个人玩儿,摇骰子摇到几然后就相应的往前走几步,看谁先到终点谁就胜利了。在玩儿的途中,地图上有很多奖励或者陷阱,有的时候运气不好,连中好几个陷阱不但没有前进反而还后退了。 这不最近再看Pandas数据读取的知识时候,我就踩了好几个小坑,幸亏把学习文档上的提供的demo进行了验证,不然在以后项目应用的时候再遇到了岂不是挺尴尬了。
|
1月前
|
数据挖掘 数据处理 索引
使用Pandas从Excel文件中提取满足条件的数据并生成新的文件
使用Pandas从Excel文件中提取满足条件的数据并生成新的文件
33 1
|
1月前
|
Python
如何使用Python的Pandas库进行数据缺失值处理?
Pandas在Python中提供多种处理缺失值的方法:1) 使用`isnull()`检查;2) `dropna()`删除含缺失值的行或列;3) `fillna()`用常数、前后值填充;4) `interpolate()`进行插值填充。根据需求选择合适的方法处理数据缺失。
70 9