How to convert data type: Object to Float/Decimal | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

11th Feb 2020, 4:52 AM
Christian Jayson Bajaro
Christian Jayson Bajaro - avatar
5 Answers
+ 2
In general, this should work data['A'] = data['A'].astype(float) https://code.sololearn.com/cBcy5u433HQ0/?ref=app
11th Feb 2020, 5:12 AM
Tibor Santa
Tibor Santa - avatar
+ 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!
11th Feb 2020, 7:20 AM
Christian Jayson Bajaro
Christian Jayson Bajaro - avatar
0
have you tried int() function
11th Feb 2020, 5:00 AM
Abhay Singh
Abhay Singh - avatar
0
Yup. I've got, "TypeError: cannot convert the series to <class 'int'>
11th Feb 2020, 5:13 AM
Christian Jayson Bajaro
Christian Jayson Bajaro - avatar
0
df[['column A', 'column B']] = df[['column A', 'column B']]. apply(pd.to_numeric) 👈Try this
11th Feb 2020, 6:37 AM
Peter Parker
Peter Parker - avatar