Home > Software design >  How to read exact numbers as displayed in excel using Pandas.read_excel or xlwrings
How to read exact numbers as displayed in excel using Pandas.read_excel or xlwrings

Time:12-20

I am new in Python programming, and I am having an issue regarding to Pandas. I've searched for similar questions using different keywords but unfortunately didn't find the right answer.

What I intended to achieve is simply read data from excel but can keep the original accuracy from Excel.

The data is Excel is like this:

excel data

I tried two ways of reading excel. One:

#using pandas
import pandas as pd
df_input=pd.read_excel(excel_path,sheet_name,dtype={'Column E':str}

Two:

#using xlwings
df_input=wb.sheets[excel_sheeet_name].used_range.options(pd.DataFrame,index=False).value

The result after running different code looks like this:

code result

I understand this is probably because of float? But How can I get '2400.0144' instead of '2400.0143999999996'?

CodePudding user response:

In this line:

import pandas as pd
df_input=pd.read_excel(excel_path,sheet_name,dtype={'Column E':str}

You are mentioning 'Column E' as a str. But I see that you are reading from a float, change it to {'Column E':float}. I hope that this fixes it.

You can also use the round function in python to round off to specific digits if the above doesn't fix the issue: here

  • Related