Python pandas: how to specify data types when reading an Excel file?

You just specify converters. I created an excel spreadsheet of the following structure:

names   ages
bob     05
tom     4
suzy    3

Where the “ages” column is formatted as strings. To load:

import pandas as pd

df = pd.read_excel('Book1.xlsx',sheetname="Sheet1",header=0,converters={'names':str,'ages':str})
>>> df
       names ages
   0   bob   05
   1   tom   4
   2   suzy  3

Leave a Comment