How to replace NaN values by Zeroes in a column of a Pandas Dataframe?

后端 未结 13 1383
无人共我
无人共我 2020-11-22 01:37

I have a Pandas Dataframe as below:

      itm Date                  Amount 
67    420 2012-09-30 00:00:00   65211
68    421 2012-09-09 00:00:00   29424
69             


        
13条回答
  •  被撕碎了的回忆
    2020-11-22 01:54

    There are two options available primarily; in case of imputation or filling of missing values NaN / np.nan with only numerical replacements (across column(s):

    df['Amount'].fillna(value=None, method= ,axis=1,) is sufficient:

    From the Documentation:

    value : scalar, dict, Series, or DataFrame Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). (values not in the dict/Series/DataFrame will not be filled). This value cannot be a list.

    Which means 'strings' or 'constants' are no longer permissable to be imputed.

    For more specialized imputations use SimpleImputer():

    from sklearn.impute import SimpleImputer
    si = SimpleImputer(strategy='constant', missing_values=np.nan, fill_value='Replacement_Value')
    df[['Col-1', 'Col-2']] = si.fit_transform(X=df[['C-1', 'C-2']])
    
    

提交回复
热议问题