Convert Pandas column containing NaNs to dtype `int`

后端 未结 17 2431
终归单人心
终归单人心 2020-11-22 11:18

I read data from a .csv file to a Pandas dataframe as below. For one of the columns, namely id, I want to specify the column type as int. The probl

17条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 11:52

    I had the problem a few weeks ago with a few discrete features which were formatted as 'object'. This solution seemed to work.

    for col in discrete:
    df[col] = pd.to_numeric(df[col], errors='coerce').astype(pd.Int64Dtype())
    

提交回复
热议问题