Replace numeric values in a pandas dataframe

后端 未结 2 802
生来不讨喜
生来不讨喜 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:52

    You can use a loop to go through each columns, and check each item. If it is an integer or float then replace it with np.nan. It can be done easily with map function applied on the column.

    you can change the condition of the if to incorporate any data type u want.

    for x in df.columns:
        df[x] = df[x].map(lambda item : np.nan if type(item) == int or type(item) == float else item)
    

    This is a naive approach and there have to be better solutions than this.!!

提交回复
热议问题