How to keep the left zero in import in Python?

1

Hello I have several TXT files with CPF numbers. The CPF has 11 numbers, so you can have leading zeros to complete that size I opened these files in Excel and put them together into one. In the CPF column, before opening each file, I made the Excel import command to Text to keep the zeros. In xlsx format worked out

But when I recorded as CSV I saw that the left zeros are gone

In Python 3 I opened, to use pandas later, and I saw that it actually lost the zero on the left and turned int64:

candidatos = pd.read_csv("candidatos_excel_cpfinteiro_csv.csv",sep=';',encoding = 'latin_1')

candidatos.info()

    <class 'pandas.core.frame.DataFrame'>
RangeIndex: 26245 entries, 0 to 26244
Data columns (total 9 columns):
UF                  26245 non-null object
Estado              26245 non-null object
Cargo               26245 non-null object
Nome_completo       26245 non-null object
CPF                 26245 non-null int64
Nome_urna           26245 non-null object
Partido             26245 non-null object
Partido_completo    26245 non-null object
Situacao            26245 non-null object
dtypes: int64(1), object(8)
memory usage: 1.0+ MB

I tried to open the Excel file directly with xlrd, but it stayed as number in the same way:

xlsx = pd.ExcelFile('candidatos_excel_cpfinteiro.xlsx')

candidatos = pd.read_excel(xlsx, 'Planilha1')

candidatos.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 26245 entries, 0 to 26244
Data columns (total 9 columns):
UF                  26245 non-null object
Estado              26245 non-null object
Cargo               26245 non-null object
Nome_completo       26245 non-null object
CPF                 26245 non-null int64
Nome_urna           26245 non-null object
Partido             26245 non-null object
Partido_completo    26245 non-null object
Situacao            26245 non-null object
dtypes: int64(1), object(8)
memory usage: 1.0+ MB

Please, would anyone have a suggestion of how to keep the zeros to the left? Or keep this column as a string?

    
asked by anonymous 23.10.2017 / 17:15

2 answers

0

Try to import direct from excel, use this line of code you will not lose any value:

import pandas as pd

your_dataframe = pd.read_excel ('name of your .xlsx file')

    
25.10.2017 / 16:49
0

You can set dtype at the time of import. For example:

candidatos = pd.read_csv("candidatos_excel_cpfinteiro_csv.csv",sep=';',
                         encoding = 'latin_1', dtype=str)

You can also set dtype individually, for example, {‘a’: np.float64, ‘b’: np.int32} .

For more details, I recommend documentation .

    
07.11.2017 / 14:08