How to replace 'any strings' with nan in pandas DataFrame using a boolean mask?

前端 未结 3 1320
自闭症患者
自闭症患者 2020-12-30 06:48

I have a 227x4 DataFrame with country names and numerical values to clean (wrangle ?).

Here\'s an abstraction of the DataFrame:

import pandas as pd
i         


        
3条回答
  •  囚心锁ツ
    2020-12-30 06:55

    Use numeric with errors coerce i.e

    cols = ['Measure1','Measure2']
    df[cols] = df[cols].apply(pd.to_numeric,errors='coerce')
    
     Country Name  Measure1  Measure2
    0          PuB       7.0       6.0
    1          JHq       2.0       NaN
    2          opE       4.0       3.0
    3          pxl       3.0       6.0
    4          ouP       NaN       4.0
    5          qZR       4.0       6.0
    

提交回复
热议问题