Is there a way to convert values like '34%' directly to int or float when using read_csv in pandas? I would like that it is directly read as 0.34.
Using this in read_csv did not work:
read_csv(..., dtype={'col':np.float})
After loading the csv as 'df' this also did not work with the error "invalid literal for float(): 34%"
df['col'] = df['col'].astype(float)
I ended up using this which works but is long winded:
df['col'] = df['col'].apply(lambda x: np.nan if x in ['-'] else x[:-1]).astype(float)/100
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…