Python dataframe change column name

pandas.DataFrame.rename#

Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed don’t throw an error.

Parameters mapper dict-like or function

Dict-like or function transformations to apply to that axis’ values. Use either mapper and axis to specify the axis to target with mapper , or index and columns .

index dict-like or function

Alternative to specifying axis ( mapper, axis=0 is equivalent to index=mapper ).

columns dict-like or function

Alternative to specifying axis ( mapper, axis=1 is equivalent to columns=mapper ).

Axis to target with mapper . Can be either the axis name (‘index’, ‘columns’) or number (0, 1). The default is ‘index’.

Читайте также:  Background это html документ

copy bool, default True

Also copy underlying data.

inplace bool, default False

Whether to modify the DataFrame rather than creating a new one. If True then value of copy is ignored.

level int or level name, default None

In case of a MultiIndex, only rename labels in the specified level.

errors , default ‘ignore’

If ‘raise’, raise a KeyError when a dict-like mapper , index , or columns contains labels that are not present in the Index being transformed. If ‘ignore’, existing keys will be renamed and extra keys will be ignored.

Returns DataFrame or None

DataFrame with the renamed axis labels or None if inplace=True .

If any of the labels is not found in the selected axis and “errors=’raise’”.

DataFrame.rename supports two calling conventions

We highly recommend using keyword arguments to clarify your intent.

Rename columns using a mapping:

>>> df = pd.DataFrame("A": [1, 2, 3], "B": [4, 5, 6]>) >>> df.rename(columns="A": "a", "B": "c">) a c 0 1 4 1 2 5 2 3 6 

Rename index using a mapping:

>>> df.rename(index=0: "x", 1: "y", 2: "z">) A B x 1 4 y 2 5 z 3 6 

Cast index labels to a different type:

>>> df.index RangeIndex(start=0, stop=3, step=1) >>> df.rename(index=str).index Index(['0', '1', '2'], dtype='object') 
>>> df.rename(columns="A": "a", "B": "b", "C": "c">, errors="raise") Traceback (most recent call last): KeyError: ['C'] not found in axis 

Using axis-style parameters:

>>> df.rename(str.lower, axis='columns') a b 0 1 4 1 2 5 2 3 6 
>>> df.rename(1: 2, 2: 4>, axis='index') A B 0 1 4 2 2 5 4 3 6 

Источник

Pandas Rename Column and Index

Pandas Rename Column and Index

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.

  • mapper: dictionary or a function to apply on the columns and indexes. The ‘axis’ parameter determines the target axis — columns or indexes.
  • index: must be a dictionary or function to change the index names.
  • columns: must be a dictionary or function to change the column names.
  • axis: can be int or string. It’s used with ‘mapper’ parameter to define the target axis. The allowed values are (‘index’, ‘columns’) or number (0, 1). The default value is ‘index’.
  • inplace: if True, the DataFrame is changed. Otherwise, a new DataFrame is returned and the current DataFrame remains unchanged. The default value is ‘False’.
  • level: can be int or level name. It’s used in case of a MultiIndex, only rename labels in the specified level.
  • errors: possible values are (‘ignore’, ‘raise’), default is ‘ignore’. If specified as ‘raise’ then KeyError is raised when a dict-like ‘mapper’, ‘index’, or ‘columns’ contains labels that are not present in the Index being transformed. If ‘ignore’, existing keys will be renamed and extra keys will be ignored.

Some important points about rename() function.

  1. It’s recommended to use keyword arguments to clearly specify the intent.
  2. We can rename single column or multiple columns with this function, depending on the values in the dictionary.

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

1. Pandas Rename Columns

import pandas as pd d1 = df = pd.DataFrame(d1) print('Source DataFrame:\n', df) # rename columns df1 = df.rename(columns=) print('Result DataFrame:\n', df1) 
Source DataFrame: Name ID Role 0 Pankaj 1 CEO 1 Lisa 2 Editor 2 David 3 Author Result DataFrame: EmpName EmpID EmpRole 0 Pankaj 1 CEO 1 Lisa 2 Editor 2 David 3 Author 

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

df1 = df.rename(mapper=, axis='columns') # axis=1 corresponds to columns 

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.

df1 = df.rename(columns=) print(df1) 
 EmpName ID Role 0 Pankaj 1 CEO 1 Lisa 2 Editor 2 David 3 Author 

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

df1 = df.rename(columns=) # same result since there is no X column 

3. Pandas Rename Indexes

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

df2 = df.rename(index=) print('Renamed Indexes:\n', df2) 
Renamed Indexes: Name ID Role #0 Pankaj 1 CEO #1 Lisa 2 Editor #2 David 3 Author 

We can also rename indexes using mapper and axis arguments.

df2 = df.rename(, axis=0) # axis='index' will work, first argument is assigned to 'mapper' 

4. Pandas Rename Single Index

df2 = df.rename(index=) print(df2) 
 Name ID Role 0 Pankaj 1 CEO #1 Lisa 2 Editor 2 David 3 Author 

5. Changing the DataFrame inplace

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

import pandas as pd d1 = df = pd.DataFrame(d1) print('Source DataFrame:\n', df) df.rename(index=, columns=, inplace=True) print('Source DataFrame:\n', df) 
Source DataFrame: Name ID Role 0 Pankaj 1 CEO 1 Lisa 2 Editor 2 David 3 Author Source DataFrame: EmpName EmpID EmpRole #0 Pankaj 1 CEO #1 Lisa 2 Editor #2 David 3 Author 

6. Using mapper function to rename columns

df = pd.DataFrame() print(df) df.rename(mapper=str.lower, axis=1, inplace=True) print(df) 
 NAME ID ROLE 0 Pankaj 1 CEO 1 Lisa 2 Editor name id role 0 Pankaj 1 CEO 1 Lisa 2 Editor 

7. Using functions to rename columns and indexes

import pandas as pd import math df = pd.DataFrame() df.rename(columns=str.lower, index=math.degrees, inplace=True) print(df) 
 name id role 0.00000 Pankaj 1 CEO 57.29578 Lisa 2 Editor 

8. Strict Rename and Raising KeyError

import pandas as pd df = pd.DataFrame() df1 = df.rename(columns=) # unmatched mappings are ignored df1 = df.rename(columns=, errors='raise') # unmatched mappings raising KeyError 
Traceback (most recent call last): File "/Users/pankaj/Documents/PycharmProjects/hello-world/journaldev/pandas/pandas_rename_column.py", line 58, in df1 = df.rename(columns=, errors='raise') KeyError: "['Salary'] not found in axis" 

9. References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Источник

Pandas Rename Column with Examples

Pandas DataFrame.rename() function is used to change the single column name, multiple columns, by index position, in place, with a list, with a dict, and renaming all columns e.t.c. We are often required to change the column name of the DataFrame before we perform any operations. In fact, changing the name of a column 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.

In this pandas article, You will learn several ways how to rename a column name of the Pandas DataFrame with examples by using functions like DataFrame.rename() , DataFrame.set_axis() , DataFrame.add_prefix() , DataFrame.add_suffix() and more.

1. Quick Examples Rename Columns of DataFrame

pandas rename column

If you are in a hurry, below are some quick examples of renaming column names in Pandas DataFrame.

3. Pandas Rename Column Name

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 the value would be your preferred column name.

Yields below output. As you see it rename the column from Courses to Courses_List .

pandas rename columns

Alternatively, you can also write the above statement by using axis=1 or axis=’columns’ .

 

In order to change columns on the existing DataFrame without copying to the new DataFrame, you have to use inplace=True .

, axis='columns', inplace=True) print(df.columns) 

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.

, inplace = True) print(df.columns) 

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

5. Rename Column by Index or Position

Renaming the column by Index/position can be done by using df.columns.values[index]='value . Index and position can be used interchanging to access columns at a given position. By using this you can rename the first column, the last column e.t.c.

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

6. Rename Columns with a List

Python list can be used to rename all columns in pandas DataFrame, the length of the list should be the same as the number of columns in the DataFrame. Otherwise, an error occurs.

7. Rename Columns in place

By default rename() function returns a new pandas DataFrame after updating the column name, you can change this behavior and rename in place by using inplace=True param.

, inplace = True) print(df.columns) 

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

8. Rename All Columns by adding Suffixes 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 prefixes and suffixes respectively to the pandas DataFrame column names.

9. Rename Column using Lambda Function

You can also change the column name using the Pandas lambda expression, This gives us more control and applies custom functions. 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 column names to either lower or upper case.

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()

Pandas String.replace() a method is used to replace a string, series, dictionary, list, number, regex, etc. from a DataFrame column. This is a very rich function as it has many variations. If you have used this syntax: df.columns.str.replace("Fee","Fee Cost") , it replaces 'Fee' column with 'Fee_Cost' .

To replace all column names.

13. Raise Error when Column Not Found

By default when rename column label is not found on Pandas DataFrame, rename() method just ignores the column. In case you wanted to throw an error when a column is not found, use errors = "raise" .

Yields error message raise KeyError("<> not found in axis".format(missing_labels)) .

 not found in axis".format(missing_labels)) KeyError: "['Cour'] not found in axis" 

14. Rename Only If the Column Exists

This example changes the Courses column to Courses_List and it doesn’t update Fees as we don’t have the Fees column. Note that even though the Fees column does not exist it didn’t raise errors even when we used errors="raise" .

 df.rename(columns=, inplace=True,errors = "raise") print(df.columns) 

Conclusion

In this article, you have learned several ways to rename column in Pandas for example renaming by index/position, multiple columns, with list and dict using the DataFrame rename(), set_axis() functions. And, changing the column name by adding prefixes or suffixes using add_prefix() & add_suffix() functions. Also learned to rename columns using user-defined functions and finally convert columns to lower and upper case. Hope you like it !!

References

You may also like reading:

Источник

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