How to rename columns in pandas

How to rename columns in pandas

How To Rename Columns In Pandas

Renaming columns in pandas DataFrames

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Introduction

Renaming dataframe columns is a common practise especially when we are interested in sharing some insights with other people and teams. This means that we may wish to make column names more meaningful so that it would be easier for readers to relate them to specific contexts.

In this short article, we will look at a few of options we have when it comes down to renaming columns of pandas DataFrames. Specifically, we are going to see how to rename columns:

First, let’s create an example DataFrame that will reference throughout this guide in order to showcase the desired pandas functionality.

Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is.

In order to rename columns using rename() method, we need to provide a mapping (i.e. a dictionary) where keys are the old column name(s) and values are the new one(s). Additionally, we must specify axis=1 in order to denote that we wish to rename columns and not the index:

Updating df.columns attribute

pandas DataFrames come with pandas.DataFrames.columns attribute which is an Index c ontaining the column labels of the DataFrame.

We can rename DataFrame columns by re-assigning this particular attribute as shown below:

Using set_axis()

pandas.DataFrame.set_axis() method can be used to assign a desired index to either the column or index axis. In order to rename the column names, make sure to provide axis=1 as shown below:

Note that in all of the examples discussed earlier, you can even use axis=’columns’ instead of axis=1 in order to denote that the operation should be effective on the column level. For example,

Final Thoughts

In today’s short guide we discussed how to rename columns of pandas DataFrames in a few different ways.

You may also be interested in understanding how to change the data types of specific columns of pandas DataFrames.

How to Rename Columns in Pandas — A Quick Guide

A short guide on multiple options for renaming columns in a pandas dataframe

Ensuring that dataframe columns are appropriately named is essential to understand what data is contained within, especially when we pass our data on to others. In this short article, we will cover a number of ways to rename columns within a pandas dataframe.

But first, what is Pandas? Pandas is a powerful, fast, and commonly used python library for carrying out data analytics. The Pandas name itself stands for “Python Data Analysis Library”. According to Wikipedia, the name originates from the term “panel data”. It allows data to be loaded in from a number file formats (CSV, XLS, XLSX, Pickle, etc.) and stored within table-like structures. These tables (dataframes) can be manipulated, analyzed, and visualized using a variety of functions that are available within pandas

Library Imports and Data Creation

The first steps involve importing the pandas library and creating some dummy data that we can use to illustrate the process of column renaming.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Renaming Columns When Loading Data

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

We will also set the inplace argument to True so that we are making the changes to the dataframe, df, directly as opposed to making a copy of it.

An alternative version of this is to specify the axis, however, it is less readable and may not be clear what this argument is doing compared to using the columns argument.

When we call upon df, we now see that our columns have been renamed from A, B, and C to Z, Y, and X respectively.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

If we want to rename specific columns, we can use the rename function again. Instead of providing a string for string mapping, we can use df.columns and select a column by providing a column index position in the square brackets. We then map this to a new column name string.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

We can also specify a mapping between an existing column name and the new one.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Renaming Columns Using Set_axis()

The next method is set_axis() which is used to set the axis (column: axis=1 or row: axis=0) of a dataframe.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

We can rename the columns directly by assigning a new list containing the names that we want to rename the columns to. This is achieved using the df.columns attribute of the dataframe.

This method requires the new list of names to be the same length as the number of columns in the dataframe. Therefore, if we only want to rename one or two columns this is probably not the best approach.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Using columns.str.replace()

In this example, we will replace column 1 with the letter Z.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Summary

Thanks for reading!

If you want to get in touch you can find me on LinkedIn or at my website.

Interested in learning more about python, petrophysics, or well log data petrophysics? Follow me on Medium or on YouTube.

How to Rename Columns in Pandas: Practice with DataFrames

You will learn how to rename the labels of columns in Pandas. This is very common when you work with data structures like DataFrames.

How can you rename columns in a Pandas DataFrame?

The Pandas DataFrame rename function allows to rename the labels of columns in a Dataframe using a dictionary that specifies the current and the new values of the labels. There are multiple ways to rename columns with the rename function (e.g. using dictionaries, normal functions or lambdas).

We will go through few examples that show how to rename the columns of a Pandas DataFrame. By the end of this tutorial this will be very clear to you.

Let’s get started!

Table of Contents

Rename a Column in a Pandas DataFrame

We will start by creating an example of Python dataframe that contains countries and their capitals. To do that we can use a Python dictionary after importing the pandas module:

Here is the dataframe we have created:

A dataframe can be also created from CSV format using the read_csv function.

To rename the columns of a Pandas dataframe we can use the rename function and pass a dictionary to it. The dictionary contains the current column names as keys and the new column names as values.

After running this command we get the following:

But then, if we print the value of the variable df we see the original columns…

To persist our change we have to assign the result of the rename function to a new dataframe:

We have seen how to update columns by name, let’s see how to print just the column names instead of the full dataframe. We can use the head function that returns the first n rows of the dataframe:

As you can see the head function prints the column header (that contains the column labels) and the first row of the dataframe.

Rename a DataFrame Column in Place

In the previous section we have seen how to rename all the columns in a dataframe by assigning the output of the rename function to a new dataframe.

With Pandas we also have the option to update dataframe columns in place, in other words we can update the original dataframe instead of creating a new one.

To update DataFrame columns in place using the Pandas rename function we have to set the inplace argument to True.

The inplace parameter is a boolean whose default value is False.

Also, if inplace is True the rename function returns None:

So, now you know two ways to update the labels of dataframe columns.

Rename One Column in a Pandas DataFrame

Pandas also allows to update one column in a dataframe.

We have updated the name of the first column simply by including only the name of the first column in the dictionary passed to the rename function.

In a similar way we can update just the second column of our dataframe.

…let’s see what happen if we try to pass to the rename function a dictionary that contains a column name that doesn’t exist.

The rename function updates the name of columns based on the dictionary passed to it only if a specific column name exists in the dataframe, otherwise it has no effect (unless the errors parameter is set to “raise”).

In this scenario, let’s see what happens if we pass an additional parameter called errors and we set its value to “raise”:

Pandas raises the following KeyError exception to tell us that there is no column called “Population”:

The default value for the errors parameter is “ignore”.

Therefore we haven’t seen any errors when the errors parameter was not present in our expression.

Rename a Column in Pandas By Position

Is it possible to rename a column in a dataframe based on its index?

Firstly we introduce the columns attribute that returns the column names of a DataFrame.

We can access the variable returned by the columns attribute as a list and use it to rename a specific column.

For example, to rename the last column we can use:

Rename DataFrame Columns with a List

Similarly, it’s also possible to assign the new column values to the .columns attribute of the DataFrame:

Keep in mind that the column names will be replaced in the order of the elements in the list provided.

Generally I prefer to always use the same way of renaming columns for consistency. My preferred way is passing a dictionary to the rename function.

Rename a Column in Pandas Using a Function

A common scenario is wanting to rename columns in a DataFrame to lowercase or uppercase.

To do that we can use Python standard functions together with the dataframe rename function.

For example, here we have used the string lower method to transform column labels into lowercase strings.

What other string methods could you use?

How to Apply a Lambda to the DataFrame Rename Function

In the previous section we have seen how apply a function to the columns of a dataframe.

Considering that lambdas are functions ( to be precise anonymous functions) we can also apply them to change the value of columns.

As you can see we are using the following lambda function…

…to set the value of the column names to their first two characters.

Renaming Index For a Pandas DataFrame

We have used the rename function to rename columns in a DataFrame. The same can be done for the index.

For instance, let’s start from the following dataframe:

I want to replace 0,1,2,3 with Nation 0, Nation 1, etc…

With the following call to the replace function I can rename the index:

To update the index of a DataFrame pass a dictionary to the index parameter of the rename function. The keys of the dictionary represent the current index and the values of the dictionary the new index.

I could also use a lambda to avoid passing that long dictionary:

Can you see how we reduce duplication using a lambda?

Before continuing, try the expression above and confirm that the result is correct.

Axis Used When Renaming Columns or Index

The rename function can also be called using a different convention.

Below you can see the generic syntax:

The mapper can be either a dictionary or a function that transforms the values of a specific axis.

All expressions update the columns in the same way

2. Rename Index

All expressions update the index in the same way:

Verify that the output of the two conventions is the same.

Change Columns and Index At The Same Time

So far we have seen how to rename either columns or index, but we can also rename both with a single expression.

Here is an example that updates both columns and index:

You can see that we have passed both parameters columns and index to the rename function.

Renaming Columns with add_prefix And add_suffix

They both return a dataframe with the updated columns.

Let’s see how they work in practice…

We will start from the following dataframe:

Apply add_prefix to the dataframe to add ‘col_’ before each column label:

And in a similar way for add_suffix:

Conclusion

Well done, you have completed this tutorial!

You now know how to rename columns in a DataFrame using Pandas. You actually have multiple ways of doing it depending on the one you prefer.

And you know how to rename the index of a DataFrame too.

We have also seen how to combine the DataFrame rename function with other Python functions including lambdas.

I have put together the source code for this tutorial so you can download it and test it on your machine.

Once again, well done!

Are getting started with Data Science? Have a look and this introduction to Data Science in Python created by DataCamp.

Pandas Tutorial: Renaming Columns in Pandas Dataframe

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

In this Pandas tutorial, we will go through how to rename columns in a Pandas dataframe. First, we will learn how to rename a single column. Second, we will go on with renaming multiple columns. In the third example, we will also have a quick look at how to rename grouped columns. Finally, we will change the column names to lowercase.

First, however, we are goign to look at a simple code example on how to rename a column in a Pandas dataframe.

Table of Contents

How to Rename a Column in Pandas Dataframe

First of all, renaming columns in Pandas dataframe is very simple: to rename a column in a dataframe we can use the rename method:

In the code example above, the column “OldName” will be renamed “NewName”. Furthermore, the use of the inplace parameter make this change permanent to the dataframe.

Why Bother with Renaming Variables?

Now, when we are working with a dataset, whether it is big data or a smaller data set, the columns may have a name that needs to be changed. For instance, if we have scraped our data from HTML tables using Pandas read_html the column names may not be suitable for our displaying our data, later. Furthermore, this is at many times part of the pre-processing of our data.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Prerequisites

Now, before we go on and learning how to rename columns, we need to have Python 3.x and Pandas installed. Now, Python and Pandas can be installed by installing a scientific Python distribution, such as Anaconda or ActivePython. On the other hand, Pandas can be installed, as many Python packages, using Pip: pip install pandas. Refer to the blog post about installing Python packages for more information.

If we install Pandas, and we get a message that there is a newer version of Pip, we can upgrade pip using pip, conda, or Anaconda navigator.

How to Rename Columns in Pandas?

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Example Data

In this tutorial, we are going to read an Excel file with Pandas to import data. More information about importing data from Excel files can be found in the Pandas read excel tutorial, previously posted on this blog.

Now, we used the index_col argument, because the first column in the Excel file we imported is the index column. If we want to get the column names from the Pandas dataframe we can use df.columns:

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

How to Rename a Single Column in Pandas

In the first example, we will learn how to rename a single column in Pandas dataframe. Note, in the code snippet below we use df.rename to change the name of the column “Subject ID” and we use the inplace=True to get the change permanent.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

In the next section, we will have a look at how to use Pandas’ rename function to rename multiple columns.

How To Rename Columns in Pandas: Example 1

To rename columns in Pandas dataframe we do as follows:

Here’s a working example on renaming columns in Pandas:

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Renaming Columns in Pandas Example 2

Another example of how to rename many columns in Pandas dataframe is to assign a list of new column names to df.columns:

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

As can be seen in the code above, we imported our data from an Excel file, we created a list with the new column names. Finally, we renamed the columns with df.columns.

Renaming Columns while Importing Data

In this section, we are going to learn how to rename columns while reading the Excel file. Now, this is also very simple. To accomplish this we create a list before we use the read_excel method. Note, we need to add a column name, in the list, for the index column.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Importantly, when changing the name of the columns while reading the data we need to know the number of columns before we load the data.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

In the next example, we are going to learn how to rename grouped columns in Pandas dataframe.

Renaming Grouped Columns in Pandas Dataframe

In this section, we are going to rename grouped columns in Pandas dataframe. First, we are going to use Pandas groupby method (if needed, check the post about Pandas groupby method for more information). Second, we are going rename the grouped columns using Python list comprehension and df.columns, among other dataframe methods.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Now, as you can see in the image above, we have a dataframe with multiple indexes. In the next code chunk, however, we are going to rename the grouped dataframe.

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Note, in the code above we also used Pandas ravel method to flatten the output to an ndarray. Now, there are other ways we can change the name of columns in Pandas dataframe. For instance, if we only want to change the column names so that the names are in lower case we can use str.lower.

Importantly, if we want the change to be permanent we need to add the inplace=True argument.

Changing Column Names in Pandas Dataframe to Lowercase

To change all column names to lowercase we can use the following code:

How to rename columns in pandas. Смотреть фото How to rename columns in pandas. Смотреть картинку How to rename columns in pandas. Картинка про How to rename columns in pandas. Фото How to rename columns in pandas

Now, this is one way to preprocess data in Python with pandas. In another post, on this blog, we can learn about data cleaning in Python with Pandas and Pyjanitor.

Video Guide: Pandas Rename Column(s)

If you prefer to learn audiovisually, here’s a YouTube Tutorial covering how to change the variable names in Pandas dataframe.

How to Rename Multi index columns in Pandas Dataframe

Pandas dataframe is a powerful two dimensional data structure which allows you to store data in rows and columns format.

It also supports multi index for columns and rows.

In this tutorials, you’ll learn how to create multi-index pandas data-frame and how to rename the columns of the multi index data-frame.

If you would like to rename columns of a single index dataframe read, How to Rename columns of Pandas Dataframe?

Creating Multi-Index Dataframe

To create a multi index dataframe, you need to follow two steps.

First, create a normal dataframe using pd.DataFrame() method.

Snippet

Exit fullscreen mode

You’ll see the below output.

Output

Exit fullscreen mode

The multilevel column index dataframe is created. a is the first level column index and b, c, d are the second level column indexes.

Next, let’s see how to rename these mutli-level columns.

Renaming the Multiindex Columns

To rename the multi index columns of the pandas dataframe, you need to use the set_levels() method.

Use the below snippet to rename the multi level columns.

Snippet

Exit fullscreen mode

Now the second level index of the columns will be renamed to b1, c1, d1 as shown below.

Output

Exit fullscreen mode

This is how you can rename the columns of the multi index dataframe.

Conclusion

In this short tutorial, you’ve learnt how to rename the columns of the multi-index pandas data-frame using the set_levels() method.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *