I have problem with converting data to float when reading excel with pandas. Here are the data in Excel (report from SAP):
6.321,68
44,33
40
540,44
python is reading data like this:
0 6.321,68
1 44.33
2 40
3 540.44
Name: AmortSAP, dtype: object
My goal is to convert these data to float as follows:
0 6321.68
1 44.33
2 40
3 540.44
I will be grateful for any form of help!
Best regards, Maciej!
df = pd.read_excel(file, usecols = use_cols, skiprows= 10, names = ['AmortSAP'], dtype= 'str', engine = 'openpyxl')
print(df['AmortSAP'])
df['AmortSAP'] = df['AmortSAP'].str.replace(" ","")
df['AmortSAP'] = df['AmortSAP'].str.replace(",","")
df['AmortSAP'] = df['AmortSAP'].astype(float)
print(df['AmortSAP'])
0 6.321,68
1 44.33
2 40
3 540.44
0 6.32168
1 44.33000
2 40.00000
3 540.44000
I try as you can see above, but the number on line “0” is obviously incorrect.
It should be 6321.68 instead of 6.32168.
Maciej Stankiewicz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.