How to replace a value in pandas, with NaN?

后端 未结 6 1674
无人共我
无人共我 2020-12-01 06:57

I am new to pandas , I am trying to load the csv in Dataframe. My data has missing values represented as ? , and I am trying to replace it with standard Missing values - NaN

6条回答
  •  执念已碎
    2020-12-01 07:33

    some times there will be white spaces with the ? in the file generated by systems like informatica or HANA

    first you Need to strip the white spaces in the DataFrame

    temp_df_trimmed = temp_df.apply(lambda x: x.str.strip() if x.dtype == "object" else x)
    

    And later apply the function to replace the data

    temp_df_trimmed['RC'] = temp_df_trimmed['RC'].map(lambda x: np.nan if x=="?"  else x)
    

提交回复
热议问题