Ignoring non-numerical string values in pandas dataframe

后端 未结 3 1000
情话喂你
情话喂你 2021-02-15 04:59

I have a DataFrame in which a column might have three kinds of values, integers (12331), integers as strings (\'345\') or some other string (\'text\').

Is there a way to

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-15 05:33

    You could use pd.to_numeric with errors=coerce to substitute your non numeric values with NaN and apply it the each column. Then you could use dropna or fillna whatever you prefer.

    df = pd.read_csv('file.csv')
    df = df.apply(pd.to_numeric, errors='coerce')
    df = df.dropna()
    

提交回复
热议问题