You were very close with your df
attempt. Try changing:
df['col'] = df['col'].astype(float)
to:
df['col'] = df['col'].str.rstrip('%').astype('float') / 100.0
# ^ use str funcs to elim '%' ^ divide by 100
# could also be: .str[:-1].astype(...
Pandas supports Python’s string processing functions on string columns. Just precede the string function you want with .str
and see if it does what you need. (This includes string slicing, too, of course.)
Above we utilize .str.rstrip()
to get rid of the trailing percent sign, then we divide the array in its entirety by 100.0 to convert from percentage to actual value. For example, 45% is equivalent to 0.45.
Although .str.rstrip('%')
could also just be .str[:-1]
, I prefer to explicitly remove the ‘%’ rather than blindly removing the last char, just in case…