I have a CSV file and it has a column full of numbers. These numbers can be formatted as 45.11 , 1,234.33, 122.33, 10,222.22 etc.
Right now they are showing up as objects in my data frame, and i need to convert them to numeric. I have tried:
df['Value'].astype(str).astype(float)
But am getting errors like this: ValueError: could not convert string to float: '1,054.43'
Does anyone know how to solve this for the weirdly formatted numbers?
CodePudding user response:
this should make the job
vals={'Value': ["45.11" , "1,234.33", "122.33", "10,222.22"]}
df = pd.DataFrame(vals)
df.Value = df.Value.apply(lambda x: x.replace(",", "")).astype(float)
print(df.Value)
output
0 45.11
1 1234.33
2 122.33
3 10222.22
Name: Value, dtype: float64