Home > front end >  Why Pandas custs the zero in the first position?
Why Pandas custs the zero in the first position?

Time:05-28

I load data to dataframe:

dfzips = pd.read_excel(filename_zips, dtype='object')

Dataframe has column with value: 00590

After load dataframe I got this as 590.

I have tried dtype='object'. Does not help me.

CodePudding user response:

Have you tried using str instead of object? if you use str (string) it maintains the zeros at the beginning.

It could be good to specify the column name you would like to change to str or object (without quotations).

1)

dfzips = pd.read_excel(filename_zips,dtype=str)

It even supports this a dict mapping where the keys constitute the column names and values the data types to be set when you want to change it. You didnt specify the column name so i just put it as "column_1"

dfzips = pd.read_excel(filename_zips,dtype={"column_1":str})

CodePudding user response:

This is the well known issue in pandas library.

try below code :

dfzips = pd.read_excel(filename_zips, dtype={'name of the column': pd.np.str})

Or:

try writing converters while reading excel.

example : df = pd.read_excel(file, dtype='string', header=headers, converters={'name of the column': decimal_converter})

function decimal_converter:

def decimal_converter(value):

try:

    return str(float(value))

except ValueError:

    return value

converters={'column name': function}

You can modify converter function according to your requirement.

Try above solutions. I hope it should work. Good Day

  • Related