zoukankan      html  css  js  c++  java
  • pandas 对某一列指定小数位数保留

    df["id"]=df["id"].round(2)

    id列只保留两位小数

    单独列还可以dtype=np.int64指定,多个列用dtype={"id":np.int64,"name":str}

    df_y = pd.DataFrame(np.random.choice(2, batch_size), dtype=np.int64, columns=["y"])

    如果是所有列:借助demicals保持高精度

    value_a = np.around(np.random.normal(0, 1, (batch_size, col)), decimals=5, out=None)


    某一列str转数字类型: pd.to_numeric(s)
        >>> 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.
  • 相关阅读:
    windows服务程序
    DevExpress之时间控件
    DevExpress之列表控件
    Docker安装及基本使用
    配置阿里云yum源
    Centos7安装gitlab
    正则表达式
    sed进阶
    初识sed和gawk
    安装saltstack
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/12710755.html
Copyright © 2011-2022 走看看