How to save a CSV from dataframe, to keep zeros left in column with numbers?
Specify dtype as string while reading the csv file as below:
# if you are reading data with leading zeros
candidatos_2014 = pd.read_csv('candidatos_2014.csv', dtype ='str')
or convert data column into string
# if data is generated in python you can convert column into string first
candidatos_2014['cpf'] = candidatos_2014['cpf'].astype('str')
candidatos_2014.to_csv('candidatos_2014.csv')
First, make sure that output in your csv file does not have zeros. If it does, but you are opening that file in Excel or another spreadsheet, you still sometimes can see values without leading zeros. In this case, Go to Data menu, then Import form Text. Excel's import utility will give you options to define each column's data type.
I am sure that it should be similar in other apps.
Hope it helps!
TLDR: you don't have to do anything if your pandas columns are type object
I feel like both answers here, but especially the accepted answer, are confusing. The short answer is that, if the dtype
of your column is object
, then pandas will write it with leading zeros. There's nothing to do.
If like me, you came here because you didn't know that for sure and when you opened the CSV, the leading zeros were gone, then follow Ivan S's advice -- take a look at the file you wrote to verify, but you should see the leading zeros there.
If you do, then both answers give guidance on how to read the data back in preserving leading zeros.
If you don't, the the datatype wasn't correct in pandas when you saved the CSV. Just changing that column using astype
wouldn't restore the zeros. You'd also need to use str.zfill
as described in this SO answer.