0
How to convert data type: Object to Float/Decimal
Hello. I tried float(), to_numeric(), convert_object, astype() but still can't make operations from column A and Column B of a Dataframe. How can I convert such datatypes? Or are there any ways to fetch the column then convert it to float? The data in the columns are both amounts with 2 decimals places. I'm 1 month new to Python. What I want to do with the columns is to get their difference. Thanks in advance! Happy Learning.
5 Respuestas
+ 2
In general, this should work
data['A'] = data['A'].astype(float)
https://code.sololearn.com/cBcy5u433HQ0/?ref=app
+ 1
Hello guys :)
Thanks for your inputs.
I've search and tried alot but this one works :) only on the line where I opening the csv file.
df = pd.read_csv('filename.csv', thousands= ',' ,decimal=b'.')
The "thousands" and "decimals" ;)
Thank you guys for the response!
0
have you tried int() function
0
Yup. I've got, "TypeError: cannot convert the series to <class 'int'>
0
df[['column A', 'column B']] = df[['column A', 'column B']]. apply(pd.to_numeric) 👈Try this