Open In App

Python | Pandas Dataframe.rename()

Last Updated : 17 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas rename() method is used to rename any index, column or row. Renaming of column can also be done by dataframe.columns = [#list]. But in the above case, there isn’t much freedom. Even if one column has to be changed, full column list has to be passed. Also, the above method is not applicable on index labels.

Syntax: DataFrame.rename(mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None)

Parameters:
mapper, index and columns: Dictionary value, key refers to the old name and value refers to new name. Only one of these parameters can be used at once.
axis: int or string value, 0/’row’ for Rows and 1/’columns’ for Columns.
copy: Copies underlying data if True.
inplace: Makes changes in original Data Frame if True.
level: Used to specify level in case data frame is having multiple level index.

Return Type: Data frame with new names

To download the CSV used in code, click here.

Example #1: Changing Index label

In this example, the name column is set as index column and it’s name is changed later using the rename() method.




# importing pandas module
import pandas as pd
  
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col ="Name" )
  
# changing index cols with rename()
data.rename(index = {"Avery Bradley": "NEW NAME",
                     "Jae Crowder":"NEW NAME 2"},
                                 inplace = True)
# display
data


Output:
As shown in the output image, the name of index labels at first and second positions were changed to NEW NAME & NEW NAME 2.

 

Example #2: Changing multiple column names

In this example, multiple column names are changed by passing a dictionary. Later the result is compared to the data frame returned by using .columns method. Null values are dropped before comparing since NaN==NaN will return false.




# importing pandas module
import pandas as pd
  
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col ="Name" )
  
  
# changing cols with rename()
new_data = data.rename(columns = {"Team": "Team Name",
                                  "College":"Education",
                                  "Salary": "Income"})
  
# changing columns using .columns()
data.columns = ['Team Name', 'Number', 'Position', 'Age',
                'Height', 'Weight', 'Education', 'Income']
  
# dropna used to ignore na values
print(new_data.dropna()== data.dropna())


Output:
As shown in the output image, the results using both ways were same since all values are True.



Previous Article
Next Article

Similar Reads

How to rename multiple column headers in a Pandas DataFrame?
Here we are going to rename multiple column headers using the rename() method. The rename method is used to rename a single column as well as rename multiple columns at a time. And pass columns that contain the new values and in place = true as an argument. We pass inplace = true because we just modify the working data frame if we pass inplace = fa
5 min read
How to rename columns in Pandas DataFrame
Given a Pandas DataFrame, let’s see how to rename columns in Pandas with examples. Here, we will discuss 5 different ways to rename column names in pandas DataFrame. How to rename columns in Pandas DataFrameMethod 1: Using rename() functionOne way of renaming the columns in a Pandas Dataframe is by using the rename() function. This method is quite
4 min read
Rename Nested Field in Spark Dataframe in Python
In this article, we will discuss different methods to rename the columns in the DataFrame like withColumnRenamed or select. In Apache Spark, you can rename a nested field (or column) in a DataFrame using the withColumnRenamed method. This method allows you to specify the new name of a column and returns a new DataFrame with the renamed column. Requ
3 min read
Python | Pandas TimedeltaIndex.rename
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.rename() function set new names on index. It defaults to returning new index. Syntax : TimedeltaIndex.rename(name,
2 min read
Python | Pandas Series.rename()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.rename() function is used to alter Series index labels or name for the given Se
2 min read
How to rename multiple columns in PySpark dataframe ?
In this article, we are going to see how to rename multiple columns in PySpark Dataframe. Before starting let's create a dataframe using pyspark: C/C++ Code # importing module import pyspark from pyspark.sql.functions import col # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and giving
2 min read
How to Rename Multiple PySpark DataFrame Columns
In this article, we will discuss how to rename the multiple columns in PySpark Dataframe. For this we will use withColumnRenamed() and toDF() functions. Creating Dataframe for demonstration: C/C++ Code # importing module import pyspark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and
2 min read
How to rename a PySpark dataframe column by index?
In this article, we are going to know how to rename a PySpark Dataframe column by index using Python. we can rename columns by index using Dataframe.withColumnRenamed() and Dataframe.columns[] methods. with the help of Dataframe.columns[] we get the name of the column on the particular index and then we replace this name with another name using the
2 min read
Rename Duplicated Columns after Join in Pyspark dataframe
In this article, we are going to learn how to rename duplicate columns after join in Pyspark data frame in Python. A distributed collection of data grouped into named columns is known as a Pyspark data frame. While handling a lot of data, we observe that not all data is coming from one data frame, thus there is a need to merge two or more data fram
4 min read
Dynamically Rename Multiple Columns in PySpark DataFrame
In this article, we are going to learn how to dynamically rename multiple columns in Pyspark data frame in Python. A data frame that is equivalent to a relational table in Spark SQL, and can be created using various functions in SparkSession is known as Pyspark data frame. While working in Pyspark, we notice numerous times the naming of columns is
13 min read