15

I am trying to export a dataframe with a column with leading zeros like this:

df["CD_LIN_NEG"]

0     001
1     001
2     004
3     001
4     001
5     001
6     003
7     006
Name: CD_LIN_NEG, dtype: object

But when I export to csv, all of the leading zeros are cut off any numbers when I open the file in Excel. How can I keep the zeros?

I have tried to convert to string but it doesn't work:

df["CD_LIN_NEG"] = df['T_PROD_CP.LN'].astype(str).apply(lambda x: x.zfill(3))

or in this way:

df["CD_LIN_NEG"] = '00' + df['T_PROD_CP.LN'].astype(str)
2
  • 6
    This looks like an excel issue, pandas will preserve the leading zeroes, you can check the csv, change the display formatting in excel
    – EdChum
    Commented Dec 20, 2016 at 10:51
  • This is an excel issue. If you open your file in notepad you will see that leading zeroes are there.
    – zipa
    Commented Dec 20, 2016 at 11:21

3 Answers 3

22

This is an excel problem as @EdChum suggested. You'll want to wrap your column in ="" with apply('="{}".format). This will tell excel to treat the entry as a formula that returns the text within quotes. That text will be your values with leading zeros.

Consider the following example.

df = pd.DataFrame(dict(A=['001', '002']))
df.A = df.A.apply('="{}"'.format)
df.to_excel('test_leading_zeros.xlsx')
7

This may not be directly relevant to the question but if the data is read from external sources via pandas.read_csv() or pandas.read_excel(), then we could specify converters for relevant columns using str.

For example,

import pandas as pd

df = pd.read_excel(
    './myexcel.xlsx',
    converters={
        "serialno": str, # Ensure serialno is read as string, maintaining leading 0's
        "location": lambda x: '-' if x=='' else str(x),
    }

df1 = pd.read_excel(
    './mycsv.csv',
    converters={
        "serialno": str, # Ensure serialno is read as string, maintaining leading 0's
        "location": lambda x: '-' if x=='' else str(x),
    }

When the data is saved to Excel or CSV files, the leading 0's are maintained.

5

The most simple solution is to just add dtype=str while reading txt or csv file in Pandas:

df = pd.read_csv(r'C:\my_folder\my_file.csv', dtype=str)
1
  • 1
    Adding dtype=str worked for pd.read_excel() also in my case.
    – ePandit
    Commented Dec 29, 2020 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.