How to rename column in pandas

How to rename column in pandas

Pandas – Rename Column Names

While working with data it may happen that you require to change the names of some or all the columns of a dataframe. In this tutorial, we’ll cover some of the different ways in Pandas to rename column names along with examples.

How to rename a column in pandas?

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

You can use the Pandas dataframe rename() function to rename column names in Pandas. There are other methods as well. The following are some of the ways in which you can change the name of columns in Pandas –

Using Pandas rename() function

The Pandas dataframe rename() function is a quite versatile function used not only to rename column names but also row indices.

You can use this function to rename specific columns. The following is the syntax to change column names using the Pandas rename() function.

Let’s now look at some examples.

Change the name of a specific column

Here, we will create a dataframe storing the category and color information of some pets in the columns “Category” and “Color” respectively.

Apply function to column names

The rename() function also accepts a function that can be applied to each column name.

In the above example, we pass a function to the rename function to modify the column names. The function gets applied to each column and gives its respective new name. Here, we split each column name on _ and use the second string as our new name for that column.

Using Pandas set_axis() function

The pandas dataframe set_axis() method can be used to rename a dataframe’s columns by passing a list of all columns with their new names. Note that the length of this list must be equal to the number of columns in the dataframe. The following is the syntax:

Let’s now look at some examples. We’ll take the same use case as above, change the column name “Category” to “Pet” in a dataframe but this time we will be using the set_axis() method.

Changing the columns attribute

You can also update a dataframe’s column by setting its columns attribute to your new list of columns. The following is the syntax:

Note that new_column_list must be of the same length as the number of columns in your dataframe.

Let’s look at an example. We’ll take the same use case as above. Create a dataframe with “Category” ad “Color” columns and then change the column name “Category” to “Pet” but this time we’ll do it by updating the columns attribute.

In the above example, we change the column names of the dataframe df by setting df.columns to a new column list. Like the set_index() function, we had to provide the list of all columns for the dataframe even if we had to change just one column name.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5

More on Pandas DataFrames –

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

How to Rename Columns in Pandas Dataframe – Definitive Guide

Pandas DataFrame is a two-dimensional data structure used to store the data in rows and column format and each column will have a headers.

You can rename the column in Pandas dataframe using the df.rename( columns= <“Old Column Name”:”New Column Name” >,inplace=True) statement.

In this tutorial, you’ll learn various methods available to rename columns in Pandas DataFrame.

If You’re in Hurry…

Here is an example of how to rename a column in pandas dataframe.

Where Columns is a dictionary containing a list of columns to be changed or renamed.

If You Want to Understand Details, Read on…

Table of Contents

Sample DataFrame

DataFrame Will Look Like

Now, let’s discuss all the methods available to rename columns in pandas.

Using rename() Method

rename() method is used to rename the columns or indexes/rows in the Pandas DataFrame.

Syntax

Where Columns is a dictionary containing a list of columns to be changed or renamed.

Using Columns Attribute

You can change the dataframe column names by directly assigning the column names to the DataFrame using columns attributes as below.

Syntax

This will directly assign the new column names to your dataframe.

Ensure that the list has the same number of values as the number of columns in the dataframe.

Example

Output

Using set_axis() Method

set_axis() method can be used to set the axis or rename columns of the dataframe.

You can set the index name when creating a dataframe using this set_axis() method.

A pandas DataFrame has two axes. where

Syntax

Example

Output

This is how you can set the column axis using the set_axis() method in the pandas dataframe.

Rename all Columns

In this section, you’ll learn how to use rename() method to change all column names in the DataFrame.

You can rename all columns in pandas DataFrame with a list or a dictionary of columns.

Syntax

Where Columns is a dictionary containing a list of columns to be changed or renamed.

Example

Output

Rename Specific Columns

In this section, you’ll learn how to rename specific column in pandas DataFrame using the rename() method.

Useful in Scenarios

Syntax

Where Columns is a dictionary containing a list of columns to be changed or renamed.

Output

You can see only the specific column Lang is replaced as Language as mentioned in the rename() method.

Rename DataFame Column by index

You can rename a column using the column index. This is also known as renaming pandas column using number or position.

Useful in Scenarios

The index is 0 based. Use 0, if you want to rename the first column.

Example

Output

You can see the column index 0 is replaced as Language as mentioned in the rename() method.

This is how you can rename pandas column by position.

Next, you’ll learn how to rename using the axis style.

Rename with Axis Style

You can rename the columns using the rename() method by using the axis keyword in it.

In this method, you’ll specify the columns as Python Set within < >rather specifying columns as a Python Dictionary with Key-Value Pairs.

This method can also be used to rename the rows/indexes of the Pandas DataFrame.

Syntax

Where Columns is specified as Python Set within the braces < >.

Example

Output

You can see all the columns of pandas DataFrame are renamed with the new names.

Next, you’ll see about the exception handling during the rename function.

Handling Errors while using rename function

In any program, exception needs to be handled appropriately to avoid the unexpected breaking of the program. In this section, we’ll show you how to handle exceptions that occur during the rename operation.

When you use rename() method, it’ll raise KeyError error, when the specified Key is not available in the DataFrame.

You can specify what needs to be done in case of such errors.

Syntax to Raise error

Example

Output

Conclusion

In this tutorial, you’ve learned various methods available to rename columns in Pandas DataFrame. Renaming Columns is also known as setting column names to Pandas Dataframe.

If you know any other methods that can be used to rename columns in pandas Dataframe, feel free to comment.

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

You’ll learn how to deal with those files that you get sent with meaningless column names. After reading this post, you’ll be able to rename your columns in a number of different ways, in order to best meeting your situation.

Table of Contents

Loading our data

Let’s begin by loading our dataset. We’ll use pandas to create the dataframe that we’ll use throughout the tutorial. Let’s get started:

This returns the following dataframe:

By printing out the df.columns attribute, all the columns are returned:

This returns the following:

We can see that there are a number of quirks with the column names (such as leading and additional spaces).

You can pass in mappers (either functions or dictionaries) or lists to change them entirely. If you do use a mapper, values must be unique (i.e., a 1-to-1 match) and ignores missing values (leaving them as-is).

How to rename a single Pandas column

To rename a single column, we can approach this in multiple ways. The easiest way would be to pass in a dictionary with just a single key:value pair.

Renaming a single column by name

This returns the following:

Renaming a single column by position

What we’ve done here is pass in the value in the first position of list of values of the column names as the key.

How to rename multiple Pandas columns

To rename multiple columns in Pandas, we can simply pass in a larger list of key:value pairs. We can even combine the two methods above.

Let’s give this a shot. We’ll rename the first column id and we’ll lower case the Age and Age Group columns.

This returns the following:

How to use a list comprehension to rename Pandas columns

There may be many times when you’re working on a large dataset and you want to streamline column names. For example, spaces can be particularly annoying when trying to use dot notation to access columns. Another common annoyance can be having confusing casing since Pandas indexing is case-sensitive.

Let’s first see how we can remove extra spaces from our columns, replace inline spaces with underscores, and lowercase all our column names.

To learn more about list comprehensions, check out my comprehensive tutorial, which is also available in video form.

This method is particularly helpful if you’re attempting to make multiple transformations consistently across all columns.

This returns the following:

What we’ve done is applied the following transformations:

Using a mapper function to rename Pandas columns

You can also use mapper functions to rename Pandas columns.

Using a lambda function to rename Pandas columns

You can also use lambda functions to pass in more complex transformations, as we did with our list comprehension. Say we wanted to replicate that example (by removing leading/trailing spaces, replacing inline spaces with underscores, and lowercasing everything), we could write:

This returns the following:

Using Inplace to Rename Pandas Columns in place

You may have noticed that for all of our examples we have reassigned the dataframe (to itself). We can avoid having to do this by using the boolean inplace= parameter in our method call. Let’s use our previous example to illustrate this:

Raising errors while renaming Pandas columns

Let’s see this in action by attempting to rename a column that doesn’t exist:

This returns the following error:

Renaming Multi-index Pandas Columns

To learn more about Pandas pivot tables, check out my comprehensive overview (complete with a video tutorial!).

This returns the following dataframe:

Conclusion

In this post, you learned about the different ways to rename columns in a Pandas dataframe. You learned how to be specific about which columns to rename, how to apply transformations to all columns, and how to rename only columns in a specific level of a MultiIndex dataframe.

Pandas Rename Column and Index

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

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

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Sometimes we want to rename columns and indexes in the Pandas DataFrame object. We can use pandas DataFrame rename() function to rename columns and indexes. It supports the following parameters.

Some important points about rename() function.

Let’s look into some examples of using Pandas rename() function.

1. Pandas Rename Columns

The above rename() function call can also be written in the following way.

It’s clear that using the keyword arguments is clearer than using the mapper and axis arguments.

2. Pandas Rename Single Column

If you want to rename a single column, just pass the single key-value pair in the columns dict parameter.

The result will be the same if there is a non-matching mapping in the columns dictionary.

3. Pandas Rename Indexes

If you want to rename indexes, pass the dict for ‘index’ parameter.

We can also rename indexes using mapper and axis arguments.

4. Pandas Rename Single Index

5. Changing the DataFrame inplace

If you want to change the source DataFrame itself, pass the inplace argument as True.

Pandas Rename Column with Examples

Table of Contents

pandas DataFrame.rename() function is used to rename the single column name, multiple columns, by index position, in place, with a list, with a dict and all columns e.t.c. We are often required to change the column name of the DataFrame before we perform any operations; in fact, rename() is one of the most searched and used functions of the Pandas.

The good thing about this function is it provides a way to rename a specific single column.

1. Quick Examples Rename Columns of DataFrame

If you are in a hurry, below are some quick examples of how to rename a column name.

Now let’s see the Syntax and examples.

2. pandas DataFrame.rename() Syntax

Following are the parameters.

Yields below output.

3. Rename Column Name Example

In order to rename a single column name on pandas DataFrame, you can use column=<> parameter with the dictionary mapping of the old name and a new name. Note that when you use column param, you cannot explicitly use axis param.

pandas DataFrame.rename() accepts a dict(dictionary) as a param for columns you wanted to rename, so you just pass a dict with key-value pair; the key is an existing column you would like to rename and value would be your preferred column name.

4. Rename Multiple Columns

You can also use the same approach to rename multiple columns of Pandas DataFrame. All you need to specify multiple columns you wanted to rename in a dictionary mapping.

Yields below output. As you see it renames multiple columns.

5. Rename Column by index or position

Rename column by Index/position can by done by using df.columns.values[index]=’value’ in pandas DataFrame. Index and position can be used interchangingly to access column at a given position. By using this you can raname first column, last column e.t.c.

As you have seen above df.columns returns a column names as a Series and df.columns.values gets column names as list, now you can set the specific index/position with a new value. The below example updates column Courses to Courses_Duration at index 3. Note that index starts from zero.

6. Rename Columns with List

python list can be used to rename all columns in pandas DataFrame, when you doesn’t want to rename any specific column then use the same name in the list. The length of the list should be same as the number of columns in the DataFrame. Otherwise, an error occurs.

Yields below output.

7. Rename Columns in place

By defaults rename() function returns a new DataFrame after updating the column names, you can change this behaviour and rename in place by using inplace=True pram.

This renames column names on DataFrame in place and returns None type.

8. Rename All Columns by adding Suffix or Prefix

Sometimes you may need to add a string text to the suffix or prefix of all column names. You can do this by getting all columns one by one in a loop and adding a suffix or prefix string.

You can also use pandas.DataFrame.add_prefix() and pandas.DataFrame.add_suffix() to add prefix and suffix respectively to the pandas DataFrame column names.

Yields below output.

9. Rename Column using Lambda Function

You can also change the column name using the pandas lambda expression, This gives us more in control and apply custom function. The below examples add’s ‘col_’ string to all column names. You can also try removing spaces from columns e.t.c

10. Rename or Convert All Columns to Lower or Upper Case

When column names are mixed with lower and upper case, it would be best practice to convert/update all columns names to either lower or upper case.

Yields below output.

11. Change Column Names Using DataFrame.set_axis()

By using DataFrame.set_axis() you can also change the column names. Note that with set_axis() you need to assign all column names. This updates the DataFrame with a new set of column names. set_axis() also used to rename pandas DataFrame Index

12. Using String replace()

Yields below output.

To replace all column names.

Yields below output.

13. Raise Error when Column Not Found

14. Rename Only If Column Exists.

Conclusion

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

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

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