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, etc. 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 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.
Related: 10 Ways to Select DataFrame Rows Based on Column Values
1. Quick Examples Rename Columns of DataFrame
If you are in a hurry, below are some quick examples of renaming column names in Pandas DataFrame.
Pandas Rename Scenario | Rename Column Example |
---|---|
Rename columns with list | df.columns=[‘A’,’B’,’C’] |
Rename column name by index | df.columns.values[2] = “C” |
Rename the column using Dict | df2=df.rename(columns={‘a’: ‘A’, ‘b’: ‘B’}) |
Rename column using Dict & axis | df2=df.rename({‘a’: ‘A’, ‘b’: ‘B’}, axis=1) |
Rename column in place | df2=df.rename({‘a’: ‘A’, ‘b’: ‘B’}, axis=’columns’) |
df.rename(columns={‘a’: ‘A’, ‘b’: ‘B’}, in place = True) | df.rename(columns={‘a’: ‘A’, ‘b’: ‘B’}, inplace = True) |
Rename using lambda function | df.rename(columns=lambda x: x[1:], inplace=True) |
Rename with error | df.rename(columns = {‘x’:’X’}, errors = “raise”) |
Rename using set_asis() | df2=df.set_axis([‘A’,’B’,’C’], axis=1) |
Now let’s see the Syntax and examples.
2. Syntax of Pandas DataFrame.rename()
Following is the syntax of the pandas.DataFrame.rename()
method, this returns either DataFrame
or None
. By default returns pandas DataFrame after renaming columns. When use inplace=True
it updates the existing DataFrame in place (self) and returns None
.
# DataFrame.rename() Syntax
DataFrame.rename(mapper=None, index=None, columns=None, axis=None,
copy=True, inplace=False, level=None, errors='ignore')
2.1 Parameters
The following are the parameters.
mapper
– dictionary or function to rename columns and indexes.index
– dictionary or function to rename index. When using with axis param, it should be (mapper, axis=0) which is equivalent to index=mapper.columns
– dictionary or function to rename columns. When using with axis param, it should be (mapper, axis=0) which is equivalent to column=mapper.axis
– Value can be either 0 or index | 1 or columns. Default set to ‘0’.copy
– Copies the data as well. Default set to True.inplace
– Used to specify the DataFrame referred to be updated. Default toFalse
. When used True, copy property will be ignored.level
– Used with MultiIndex. Takes Integer value. Default set toNone
.errors
– Take valuesraise
orignore
. if ‘raise’ is used, raise a KeyError when a dict-like mapper, index, or column contains labels that are not present in the Index being transformed. If ‘ignore’ is used, existing keys will be renamed and extra keys will be ignored. Default set toignore
.
Let’s create a DataFrame with a dictionary of lists, our pandas DataFrame contains column names Courses
, Fee
, Duration
.
import pandas as pd
technologies = ({
'Courses':["Spark","PySpark","Hadoop","Python","pandas","Oracle","Java"],
'Fee' :[20000,25000,26000,22000,24000,21000,22000],
'Duration':['30day', '40days' ,'35days', '40days', '60days', '50days', '55days']
})
df = pd.DataFrame(technologies)
print(df.columns)
Yields below output.
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 want to rename, so you just pass a dict with a key-value pair; the key is an existing column you would like to rename and the value would be your preferred column name.
# Rename a Single Column
df2=df.rename(columns = {'Courses':'Courses_List'})
print(df2.columns)
Yields below output. As you see it rename the column from Courses
to Courses_List
.
Alternatively, you can also write the above statement by using axis=1 or axis='columns'
.
# Alternatively you can write above code using axis
df2=df.rename({'Courses':'Courses_List'}, axis=1)
df2=df.rename({'Courses':'Courses_List'}, axis='columns')
In order to change columns on the existing DataFrame without copying to the new DataFrame, you have to use inplace=True
.
# Replace existing DataFrame (inplace). This returns None.
df.rename({'Courses':'Courses_List'}, 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 want to rename in a dictionary mapping.
Related: In Pandas you can rename specific column of DataFrame.
# Rename multiple columns
df.rename(columns = {'Courses':'Courses_List','Fee':'Courses_Fee',
'Duration':'Courses_Duration'}, inplace = True)
print(df.columns)
Yields below output. As you see it renames multiple columns.
5. Rename the Column by Index or Position
To rename the column by Index/position can be done by using df.columns.values[index]='value
. Index or position can be used interchanging to access columns at a given position. By using this you can rename the first column to the last column.
As you can see from the 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.
# Pandas rename column by index
df.columns.values[2] = "Courses_Duration"
print(df.columns)
# Output:
# Index(['Courses', 'Fee', 'Courses_Duration'], dtype='object')
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.
# Rename columns with list
column_names = ['Courses_List','Courses_Fee','Courses_Duration']
df.columns = column_names
print(df.columns)
Yields below output.
# Output:
Index(['Courses_List', 'Courses_Fee', 'Courses_Duration'], dtype='object')
7. Rename Columns Inplace
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.
# Rename multiple columns
df.rename(columns = {'Courses':'Courses_List','Fee':'Courses_Fee',
'Duration':'Courses_Duration'}, 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.
# Rename All Column Names by adding Suffix or Prefix
df.columns = column_names
df.columns = ['col_'+str(col) for col in df.columns]
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.
# Add prefix to the column names
df2=df.add_prefix('col_')
print(df2.columns)
# Add suffix to the column names
df2=df.add_suffix('_col')
print(df2.columns))
Yields below output.
# Output:
Index(['col_Courses', 'col_Fee', 'col_Duration'], dtype='object')
Index(['Courses_col', 'Fee_col', 'Duration_col'], dtype='object')
9. Rename the Column using the 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 a ‘col_’ string to all column names. You can also try removing spaces from columns etc.
# Rename using Lambda function
df.rename(columns=lambda x: 'col_'+x, inplace=True)
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.
# Change to all lower case
df = pd.DataFrame(technologies)
df2=df.rename(str.lower, axis='columns')
print(df2.columns)
# Change to all upper case
df = pd.DataFrame(technologies)
df2=df.rename(str.upper, axis='columns')
print(df2.columns)
Yields below output.
# Output:
Index(['courses', 'fee', 'duration'], dtype='object')
Index(['COURSES', 'FEE', 'DURATION'], dtype='object')
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
# Change column name using set_axis()
df.set_axis(['Courses_List', 'Course_Fee', 'Course_Duration'], axis=1, inplace=True)
print(df.columns)
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", "Courses_Fee")
, it replaces 'Fee'
column with 'Courses_Fee'
.
# Change column name using String.replace()
df.columns = df.columns.str.replace("Fee","Courses_Fee")
print(df.columns)
Yields below output.
# Output:
Index(['Courses', 'Courses_Fee', 'Duration'], dtype='object')
To replace all column names in a DataFrame using str.replace() method. You can define DataFrame with column names('Courses_List'
, 'Courses_Fee'
, 'Courses_Duration'
) and then apply str.replace() method over the columns of DataFrame it will replace underscores("_"
) with white space(" "
).
# Rename all column names
df.columns = df.columns.str.replace("_"," ")
print(df.columns)
Yields below output.
# Output:
Index(['Courses List', 'Course Fee', 'Course Duration'], dtype='object')
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"
.
# Throw Error when Rename column doesn't exists.
df.rename(columns = {'Cour':'Courses_List'}, errors = "raise")
Yields error message raise KeyError("{} not found in axis".format(missing_labels))
.
# Output:
raise KeyError("{} 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"
.
# Change column only if column exists.
df = pd.DataFrame(technologies)
d={'Courses':'Courses_List','Fees':'Courses_fees'}
df.rename(columns={k: v for k, v in d.items() if k in df.columns}, inplace=True,errors = "raise")
print(df.columns)
Conclusion
In this article, you have learned several ways to rename columns in Pandas for example renaming by index/position, multiple columns, with list and dict using the DataFrame rename(), and 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 !!
You can use the rename()
method with a dictionary to rename a single column. you can pass column={}
parameter into this function with the dictionary mapping of the old column name and a new column name.
You can rename multiple columns using the rename() method by providing a dictionary where the keys are the old column names, and the values are the new column names.
You can use the str.replace
method to rename columns based on a specific condition or pattern using regular expressions. For example, to remove spaces and replace them with underscores.
Yes, you can rename columns based on their position using the df.columns
attribute and indexing.
Happy Learning !!
Related Articles
- Pandas Rename Row Index Values of DataFrame
- Pandas Rename Index Name of DataFrame
- Different Ways to Iterate Over Pandas DataFrame Rows
- How to transform or remap Pandas DataFrame column values with Dict
- Several Ways to Remove Pandas DataFrame Columns
- Pandas Rename Index Values of DataFrame
- Pandas Rename Index of DataFrame
- How to Rename a Pandas Series?
- How to Rename Columns With List in pandas?