read_excel() function auto convert int to float

Viewed 1008

when I am reading the file using read_excel() function integer columns are auto-converted into the float datatype in pandas python when any NAN value present. like in below example:

A B C D
1 2 s 3.9
        
3 4 d 2.0

when I am reading the file using read_excel().

Dataframe is like this
A   B   C  D
1.0 2.0 s 3.9
Nan Nan Nan nan
3.0 4.0 d  2.0

can anyone suggest me how to stop this auto conversion from int to float while reading the file in pandas

2 Answers

You can set dtype=object to avoid that.

import pandas as pd
df=pd.read_excel('sample.xlsx')
df
     A    B    C    D
0  1.0  2.0    s  3.9
1  NaN  NaN  NaN  NaN
2  3.0  4.0    d  2.0
df=pd.read_excel('sample.xlsx', dtype=object)
df
     A    B    C    D
0    1    2    s  3.9
1  NaN  NaN  NaN  NaN
2    3    4    d  2.0

try this :

df=pd.read_excel("path to your file",na_filter=False)

output:

df
Out[71]: 
          A  B  C    D
0         1  2  s  3.9
1                     
2         3  4  d    2
Related