How to change the type of a column in Pandas?

Asked

Viewed 7,021 times

2

I want to change the type of a column of a CSV file. I used this command:

cand_doacoes['CPF_CNPJ_doador'] = cand_doacoes.CPF_CNPJ_doador.astype(int64)

But the error appears:

NameError                                 Traceback (most recent call last)
<ipython-input-33-842f431fca9e> in <module>()
----> 1 cand_doacoes['CPF_CNPJ_doador'] = cand_doacoes.CPF_CNPJ_doador.astype(int64)

NameError: name 'int64' is not defined

Someone knows the right command?

3 answers

2

I did some tests and if it is informed 'int64' between quotes(string) and conversion occurs smoothly.

cand_doacoes.CPF_CNPJ_doador.astype('int64')
  • Thank you. It really worked But it gave this error after: Valueerror: Cannot Convert non-finite values (NA or inf) to integer I think that’s why in this column there are blank lines. I cannot delete these lines. Is the solution to convert the blank lines to "0"? Or is there some other command?

  • I’ll run some tests here and then give feedback.

  • The values of the Cpf_cnpj_donor column are only numbers, without the points?

  • 1

    If the column contains only numbers, vc could loop before the conversion by filling the empty cells with '0', thus: cand_doacoes['Cpf_cnpj_doador'] = [x if x.isdigit() Else '0' for x in cand_doacoes.Cpf_cnpj_doador]

0

You can use several types, I usually use the:

All-purpose

df.NOME_DA_COLUNA = pd.to_numeric(df.NOME_DA_COLUNA , errors='coerce')

To format Date in String

import datetime
from datetime import date
df["DT_NASC"] = pd.to_datetime(df["DT_NASC"], errors='coerce').dt.strftime("%d/%m/%Y")

To format Column for String

df['NOME_DA_COLUNA '] = df['NOME_DA_COLUNA '].astype(str)

0

You can fill in the missing data with 0 zeros using the method fillna

cand_doacoes['CPF_CNPJ_doador'].fillna(0)

This form is easier. Then you change the name and/or type of the column.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.