Replace numeric values in a pandas dataframe

后端 未结 2 817
生来不讨喜
生来不讨喜 2021-01-17 01:16

Problem: Polluted Dataframe.
Details: Frame consists of NaNs string values which i know the meaning of and numeric values.
Task

2条回答
  •  [愿得一人]
    2021-01-17 01:45

    You can do a round-conversion to str to replace the values and back.

    df.astype('str').replace({'\d+': np.nan, 'nan': np.nan}, regex=True).astype('object')
    #This makes sure already existing np.nan are not lost
    

    Output

        0   1   2
    0   abc cdf NaN
    1   k   sum some
    2   NaN NaN nothing
    

提交回复
热议问题