How to change the name of the pandas dataframe column?

Asked

Viewed 19,845 times

7

I am using pandas to process a CSV file in the following structure:

nome;idade
Fulano;28
Joao da Silva;27
Maria;29

The reading of the file is done as follows:

import pandas as pd

df = pd.read_csv('dados.csv', sep=';')

My dataframe is generated df with the content of the CSV:

            nome  idade
0         Fulano     28
1  Joao da Silva     27
2          Maria     29

So, I would like to know how I can change the name of the columns in my dataframe? I tried to use the function rename in this way:

df.rename(columns={'nome': 'nome_completo'})

but apparently it didn’t work.

I’d like to change the name of the column nome for nome_completo in the dataframe.

  • Cat, if any answer solved your problem, you could mark one of them as accepted. Understand the importance of this link: https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-reply

6 answers

9


According to the documentation of command rename, return of this function is a new Dataframe with the renamed column(s) (s).
To get to the desired answer, (1) simply assign the function return to a new variable, or (2) pass the command inplace = True that it will assign the new name of the direct column in the Dataframe that you have.

df = df.rename(columns={'nome': 'nome_completo'})
#ou
df.rename(columns={'nome': 'nome_completo'}, inplace = True)

It is also possible to rename columns by passing only a dictionary to the function .rename() along with the parameter Axis=1, in this way:

df = df.rename({'nome': 'nome_completo'}, axis = 1)

5

You can pass a list with the new names you want:

df.columns = ['nome_completo', 'idade']

Just an addendum, the command df.rename(columns = {'nome':'nome_completo'}) is correct, but for the change to be implemented in the df you need to add the argument inplace = True.

3

Try it like this

import pandas as pd

df = pd.read_csv('dados.csv', sep=';', names=['nome_completo', 'idade'], header=0)

depending on your csv file, you may need to assign header=1 to omit the previous header

so you don’t need to add any more rows

  • 1

    Good William, but remember that the header has to be 0 ;)

  • depends on your csv file, as I do not have could not do the test to check the most correct way to your problem, but it is a way to achieve your goal being necessary to adapt according to your expected output, but if you think it would be better to answer with header=0, I can leave the way that best solved your problem

3

You can name the columns as soon as calling the function read_csv pandas:

df = pd.read_csv('dados.csv', delimiter=';', encoding='iso-8859-1', usecols=['nome_completo', 'idade'])

besides naming, can add extra parameters like delimiter and encoding.

0

You just forgot to put Unique = True at the end.

df.Rename(Columns={'name': 'full name'}, Unique = True)

That way it’ll work!!

-2

You can use too:

df.rename({"nome": "nome_completo"}, axis=1, inplace=True)

Browser other questions tagged

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