美文网首页我爱编程
Pandas处理丢失数据

Pandas处理丢失数据

作者: 闫_锋 | 来源:发表于2018-07-25 11:31 被阅读48次

    转载:
    https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/3-4-pd-nan/

    dates = pd.date_range('20130101', periods=6)
    df = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates, columns=['A','B','C','D'])
    df.iloc[0,1] = np.nan
    df.iloc[1,2] = np.nan
    """
                 A     B     C   D
    2013-01-01   0   NaN   2.0   3
    2013-01-02   4   5.0   NaN   7
    2013-01-03   8   9.0  10.0  11
    2013-01-04  12  13.0  14.0  15
    2013-01-05  16  17.0  18.0  19
    2013-01-06  20  21.0  22.0  23
    """
    

    pd.dropna()

    df.dropna(
        axis=0,     # 0: 对行进行操作; 1: 对列进行操作
        how='any'   # 'any': 只要存在 NaN 就 drop 掉; 'all': 必须全部是 NaN 才 drop 
        ) 
    """
                 A     B     C   D
    2013-01-03   8   9.0  10.0  11
    2013-01-04  12  13.0  14.0  15
    2013-01-05  16  17.0  18.0  19
    2013-01-06  20  21.0  22.0  23
    """
    

    pd.fillna()

    df.fillna(value=0)
    """
                 A     B     C   D
    2013-01-01   0   0.0   2.0   3
    2013-01-02   4   5.0   0.0   7
    2013-01-03   8   9.0  10.0  11
    2013-01-04  12  13.0  14.0  15
    2013-01-05  16  17.0  18.0  19
    2013-01-06  20  21.0  22.0  23
    """
    

    pd.isnull()

    df.isnull() 
    """
                    A      B      C      D
    2013-01-01  False   True  False  False
    2013-01-02  False  False   True  False
    2013-01-03  False  False  False  False
    2013-01-04  False  False  False  False
    2013-01-05  False  False  False  False
    2013-01-06  False  False  False  False
    """
    
    np.any(df.isnull()) == True  
    # True
    

    相关文章

      网友评论

        本文标题:Pandas处理丢失数据

        本文链接:https://www.haomeiwen.com/subject/ehhpmftx.html