How to set a cell to NaN in a pandas dataframe

前端 未结 8 970
时光取名叫无心
时光取名叫无心 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:57

    While using replace seems to solve the problem, I would like to propose an alternative. Problem with mix of numeric and some string values in the column not to have strings replaced with np.nan, but to make whole column proper. I would bet that original column most likely is of an object type

    Name: y, dtype: object
    

    What you really need is to make it a numeric column (it will have proper type and would be quite faster), with all non-numeric values replaced by NaN.

    Thus, good conversion code would be

    pd.to_numeric(df['y'], errors='coerce')
    

    Specify errors='coerce' to force strings that can't be parsed to a numeric value to become NaN. Column type would be

    Name: y, dtype: float64
    

提交回复
热议问题