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
0 5.2
1 na
2 2.2
3 7.6
4 7.5
5 3.0
...
I import my data from a csv file like so:
df = DataFrame(pd.read_csv(myfile.file))
Unfortunately, when I do this, the column 'cap' is imported entirely as strings. I would like floats to be identified as floats and strings as strings. Trying to convert this using:
df['cap'] = df['cap'].astype(float)
throws up an error:
could not convert string to float: na
Is there any way to make all the numbers into floats but keep the 'na' as a string?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…