Python pandas dataframe rename column

How to Rename Pandas DataFrame Column in Python

Pandas is a Python library for data analysis and manipulation. Almost all operations in pandas revolve around DataFrame s.

A Dataframe is is an abstract representation of a two-dimensional table which can contain all sorts of data. They also enable us give all the columns names, which is why oftentimes columns are referred to as attributes or fields when using DataFrames .

In this article we’ll see how we can rename an already existing DataFrame ‘s columns.

There are two options for manipulating the column names of a DataFrame :

  1. Renaming the columns of an existing DataFrame
  2. Assigning custom column names while creating a new DataFrame

Let’s take a look at both of the methods.

Renaming Columns of an Existing Dataframe

We have a sample DataFrame below:

import pandas as pd data = 'Name':['John', 'Doe', 'Paul'], 'age':[22, 31, 15]> df = pd.DataFrame(data) 

The DataFrame df looks like this:

To rename the columns of this DataFrame , we can use the rename() method which takes:

  1. A dictionary as the columns argument containing the mapping of original column names to the new column names as a key-value pairs
  2. A boolean value as the inplace argument, which if set to True will make changes on the original Dataframe
Читайте также:  Store engine in php

Let us change the column names in our DataFrame from Name, age to First Name, Age .

df.rename(columns = 'Name' : 'First Name', 'age' : 'Age'>, inplace = True) 

Assign Column Names While Creating a Dataframe

Now we will discuss how to assign column names while creating a DataFrame .

This is particularly helpful when you are creating a DataFrame from a csv file and want to ignore the header column names and assign your own.

By passing a list to the names argument, we can override the already existing header column with our own. The list must have a name for every column in the data, otherwise, an exception is thrown.

Note that if we want to rename only a few columns, it is better to use the rename method on the DataFrame after creating it.

We will be creating a DataFrame using out.csv , which has the following contents:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Name, age John, 22 Doe, 31 Paul, 15 

Note that the first line in the file is the header line and contains the column names. Pandas, by default, assigns the column names to the DataFrame from the first line.

Hence, we will specify to ignore the header line while creating our DataFrame and specify the column names in a list that is passed to the names argument:

columns = ['First Name', 'Age'] df = pd.read_csv('out.csv', header = None, names = columns) df 

Another way of doing this is by specifying the column names in the plain old DataFrame() constructor.

The one difference being that now the parameter that takes the list of column names is called column instead of names :

import numpy as np new_columns = ['First Name', 'Age'] data = np.array([["Nicholas", 23],["Scott", 32],["David", 25]]) df = pd.DataFrame(data, columns = new_columns) 

This results in a different DataFrame :

Conclusion

In this article we’ve quickly gone over how we can name and rename columns in DataFrame s. Either by assigning names while constructing the DataFrame instance, or by renaming them after the fact with the rename() method.

Источник

Как переименовать столбцы в Pandas (с примерами)

Вы можете использовать один из следующих трех методов для переименования столбцов в кадре данных pandas:

Способ 1: переименовать определенные столбцы

df.rename(columns = , inplace = True ) 

Способ 2: переименовать все столбцы

df.columns = ['new_col1', 'new_col2', 'new_col3', 'new_col4'] 

Способ 3: заменить определенные символы в столбцах

df.columns = df.columns.str.replace('old_char', 'new_char') 

В следующих примерах показано, как использовать каждый из этих методов на практике.

Способ 1: переименовать определенные столбцы

В следующем коде показано, как переименовать определенные столбцы в кадре данных pandas:

import pandas as pd #define DataFrame df = pd.DataFrame() #list column names list(df) ['team', 'points', 'assists', 'rebounds'] #rename specific column names df.rename(columns = , inplace = True ) #view updated list of column names list(df) ['team_name', 'points_scored', 'assists', 'rebounds'] 

Обратите внимание, что столбцы «команда» и «очки» были переименованы, а имена всех остальных столбцов остались прежними.

Способ 2: переименовать все столбцы

В следующем коде показано, как переименовать все столбцы в кадре данных pandas:

import pandas as pd #define DataFrame df = pd.DataFrame() #list column names list(df) ['team', 'points', 'assists', 'rebounds'] #rename all column names df.columns = ['_team', '_points', '_assists', '_rebounds'] #view updated list of column names list(df) ['_team', '_points', '_assists', '_rebounds'] 

Обратите внимание, что этот метод быстрее использовать, если вы хотите переименовать большинство или все имена столбцов в DataFrame.

Способ 3: заменить определенные символы в столбцах

В следующем коде показано, как заменить определенный символ в имени каждого столбца:

import pandas as pd #define DataFrame df = pd.DataFrame() #list column names list(df) ['team', 'points', 'assists', 'rebounds'] #rename $ with blank in every column name df.columns = df.columns.str.replace('$', '') #view updated list of column names list(df) ['team', 'points', 'assists', 'rebounds'] 

Обратите внимание, что этот метод позволил нам быстро удалить «$» из имени каждого столбца.

Дополнительные ресурсы

В следующих руководствах объясняется, как выполнять другие распространенные операции в pandas:

Источник

How to Rename a Column in Pandas – Python Pandas Dataframe Renaming Tutorial

Ihechikara Vincent Abba

Ihechikara Vincent Abba

A Pandas Dataframe is a 2-dimensional data structure that displays data in tables with rows and columns.

In this article, you’ll learn how to rename columns in a Pandas Dataframe by using:

How to Rename a Column in Pandas Using the rename() Function

In this section, you’ll see a practical example of renaming a Pandas Dataframe using the rename() function.

Let’s begin by passing data into a Dataframe object:

import pandas as pd students = < "firstname": ["John", "Jane", "Jade"], "lastname": ["Doe", "Done", "Do"] ># convert student names into a Dataframe df = pd.DataFrame(students) print(df)
# Output firstname lastname 0 John Doe 1 Jane Done 2 Jade Do

In the example above, we created a Python dictionary which we used to store the firstname and lastname of students.

We then converted the dictionary to a Dataframe by passing it as a parameter to the Pandas Dataframe object: pd.DataFrame(students) .

When printed to the console, we had this table printed out:

 firstname lastname 0 John Doe 1 Jane Done 2 Jade Do

The goal here is to rename the columns. We can do that using the rename() function.

Here’s what the syntax looks like:

Let’s go ahead and change the column names ( firstname and lastname ) in the table from lowercase to uppercase ( FIRSTNAME and LASTNAME ).

import pandas as pd students = < "firstname": ["John", "Jane", "Jade"], "lastname": ["Doe", "Done", "Do"] ># convert student names into a Dataframe df = pd.DataFrame(students) df.rename(columns=, inplace=True) print(df)
# Output FIRSTNAME LASTNAME 0 John Doe 1 Jane Done 2 Jade Do

In the code above, we specified that the columns firstname and lastname should be renamed to FIRSTNAME and LASTNAME , respectively: df.rename(columns=, inplace=True)

You’ll notice that we added the inplace=True parameter. This helps in persisting the new changes in the Dataframe. Delete the parameter and see what happens 😉

You can rename the columns to whatever you want. For instance, we can use SURNAME instead of lastname by doing this:

import pandas as pd students = < "firstname": ["John", "Jane", "Jade"], "lastname": ["Doe", "Done", "Do"] ># convert student names into a Dataframe df = pd.DataFrame(students) df.rename(columns=, inplace=True) print(df)
# Output FIRSTNAME SURNAME 0 John Doe 1 Jane Done 2 Jade Do

You can change just one column name, too. You are not required to change all the column names at the same time.

How to Rename a Column in Pandas Using a List

You can access the column names of a Dataframe using df.columns . Consider the table below:

 firstname lastname 0 John Doe 1 Jane Done 2 Jade Do

We can print out the column names with the code below:

print(df.columns) # Index(['firstname', 'lastname'], dtype='object')

Using that, we can rename the column of a Dataframe. Here’s an example:

import pandas as pd students = < "firstname": ["John", "Jane", "Jade"], "lastname": ["Doe", "Done", "Do"] ># convert student names into a Dataframe df = pd.DataFrame(students) df.columns = ["FIRSTNAME", "SURNAME"] print(df)
# Output FIRSTNAME SURNAME 0 John Doe 1 Jane Done 2 Jade Do

In the example above, we put the new column names in a List and assigned it to the Dataframe columns: df.columns = [«FIRSTNAME», «SURNAME»] .

This will override the previous column names.

How to Rename a Column in Pandas Using the set_axis() Function

The syntax for renaming a column with the set_axis() function looks like this:

df.set_axis([NEW_COLUMN_NAME. ], axis="columns")
import pandas as pd students = < "firstname": ["John", "Jane", "Jade"], "lastname": ["Doe", "Done", "Do"] ># convert student names into a Dataframe df = pd.DataFrame(students) df.set_axis(["FIRSTNAME", "SURNAME"], axis="columns", inplace=True) print(df)
# Output FIRSTNAME SURNAME 0 John Doe 1 Jane Done 2 Jade Do

Note that the inplace=True parameter might raise a warning because it’s deprecated for the set_axis() function and will be replaced in the future.

Summary

In this article, we talked about renaming a column in Pandas.

We saw different methods that can be used to rename a Pandas Dataframe column with code examples.

Источник

Оцените статью