How to set a cell to NaN in a pandas dataframe

前端 未结 8 959
时光取名叫无心
时光取名叫无心 2020-12-04 09:51

I\'d like to replace bad values in a column of a dataframe by NaN\'s.

mydata = {\'x\' : [10, 50, 18, 32, 47, 20], \'y\' : [\'12\', \'11\', \'N/A\', \'13\', \         


        
8条回答
  •  既然无缘
    2020-12-04 09:55

    You can use replace:

    df['y'] = df['y'].replace({'N/A': np.nan})
    

    Also be aware of the inplace parameter for replace. You can do something like:

    df.replace({'N/A': np.nan}, inplace=True)
    

    This will replace all instances in the df without creating a copy.

    Similarly, if you run into other types of unknown values such as empty string or None value:

    df['y'] = df['y'].replace({'': np.nan})
    
    df['y'] = df['y'].replace({None: np.nan})
    

    Reference: Pandas Latest - Replace

提交回复
热议问题