python: convert numerical data in pandas dataframe to floats in the presence of strings

后端 未结 4 1355
难免孤独
难免孤独 2020-12-18 21:58

I\'ve got a pandas dataframe with a column \'cap\'. This column mostly consists of floats but has a few strings in it, for instance at index 2.

df =
    cap
         


        
4条回答
  •  独厮守ぢ
    2020-12-18 22:46

    I tried an alternative on the above:

    for num, item in enumerate(data['col']):
        try:
            float(item)
        except:
            data['col'][num] = nan
    

提交回复
热议问题