Tuesday, 21 May 2019

pandas read_excel into string format

when you use pandas to load a sheet, if the sheet has numbers like this:

upc
001
003320

after loaded, you will get number like this:
upc
1.0
3320.0

you want to keep prefix"00"


just FYI, below solution doesn't work

p['upc'] = p['upc'].astype('str')

above solution will convert numbers to :

upc
1
3320

you still cannot get 00 back because they already deleted when you were loading them from the sheet.

so you should keep them when you were loading by :

p= pd.read_excel('po-2019-05-02 copy.xlsx',converters={'po':str,'store':str,'dc':str,'upc':str})
or
p= pd.read_excel('po-2019-05-02 copy.xlsx', dtype=str)

No comments:

Post a comment