Convert Pandas column containing NaNs to dtype `int`

后端 未结 17 2421
终归单人心
终归单人心 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:53

    If you can modify your stored data, use a sentinel value for missing id. A common use case, inferred by the column name, being that id is an integer, strictly greater than zero, you could use 0 as a sentinel value so that you can write

    if row['id']:
       regular_process(row)
    else:
       special_process(row)
    

提交回复
热议问题