Remove rows where column value type is string Pandas

前端 未结 4 2020
暗喜
暗喜 2020-12-17 10:21

I have a pandas dataframe. One of my columns should only be floats. When I try to convert that column to floats, I\'m alerted that there are strings in there. I\'d like to d

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 11:08

    Assume your data frame is df and you wanted to ensure that all data in one of the column of your data frame is numeric in specific pandas dtype, e.g float:

    df[df.columns[n]] = df[df.columns[n]].apply(pd.to_numeric, errors='coerce').fillna(0).astype(float).dropna()
    

提交回复
热议问题