JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Pandas rename multiple columns

Author:JIYIK Last Updated:2025/05/01 Views:

DataFrame is a two-dimensional labeled data structure. It is a heterogeneous data structure with variable size.

A DataFrame contains labeled axes called rows and columns.

This tutorial will discuss different ways to rename multiple columns of a DataFrame using python.


Use Pandas's rename()function to rename multiple columns

The Pandas library provides rename()functions for renaming DataFrame columns.

rename()The function takes a mapperDataFrame, which is a dictionary-like data structure containing the renamed columns as keys and the names as values. It returns a DataFrame.

In-place modifications can also be inplace = Truedone via settings.

grammar:

pandas.rename(mapper)

Following are rename()the steps to rename multiple columns using method.

  • Import the Pandas library.
  • Pass the Mapper to rename()the method.
  • rename()The method will return a data frame with the column renamed.
  • Print the DataFrame.

The following code is the implementation of the above method.

# importing pandas library
import pandas as pd

# creating a dataframe
df = pd.DataFrame(
    {
        "course": ["C", "Python", "Java"],
        "Mentor": ["alex", "alice", "john"],
        "cost": [1000, 2000, 3000],
    }
)

# Dataframe before renaming
print("\n Before Renaming")
print(df)

# renaming the multiple columns by index
df = df.rename(columns={df.columns[0]: "subject", df.columns[2]: "price"})

# Dataframe after renaming
print("\n After Renaming")
print(df)

Output before renaming:

course Mentor cost
C Alex 1000
Python alice 2000
Java john 3000

Output after renaming:

subject Mentor price
C Alex 1000
Python alice 2000
Java john 3000

DataFrame.column.valuesRenaming multiple columns using Pandas

DataFrame.column.valuesAll column names will be returned, we can use the index to modify the column name. column.valuesAn array of indexes will be returned.

Here are the steps to rename multiple columns using this method:

  1. Import the Pandas library.
  2. Use DataFrame.column.valuesto retrieve an array of column names.
  3. Change the name of a column by passing its index.
  4. Print the data frame.

The following code is the implementation of the above method.

# importing pandas library
import pandas as pd

# creating a dataframe
df = pd.DataFrame(
    {
        "course": ["C", "Python", "Java"],
        "Mentor": ["alex", "alice", "john"],
        "cost": [1000, 2000, 3000],
    }
)

# Dataframe before renaming
print("\n Before Renaming")
print(df)

# renaming the multiple columns by index
df.columns.values[0:2] = ["Subject", "Teacher"]

# Dataframe after renaming
print("\n After Renaming")
print(df)

Output before renaming:

course Mentor cost
C Alex 1000
Python alice 2000
Java john 3000

Output after renaming:

Subject Teacher cost
C Alex 1000
Python alice 2000
Java john 3000

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert Pandas to CSV without index

Publish Date:2025/05/01 Views:159 Category:Python

As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates

Convert Pandas DataFrame to Dictionary

Publish Date:2025/05/01 Views:197 Category:Python

This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan

Convert Pandas DataFrame columns to lists

Publish Date:2025/05/01 Views:191 Category:Python

When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m

Subtracting Two Columns in Pandas DataFrame

Publish Date:2025/05/01 Views:120 Category:Python

Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi

Dropping columns by index in Pandas DataFrame

Publish Date:2025/05/01 Views:99 Category:Python

DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns

Pandas Copy DataFrame

Publish Date:2025/05/01 Views:53 Category:Python

This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:

Pandas DataFrame.ix[] Function

Publish Date:2025/05/01 Views:168 Category:Python

Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice

Pandas DataFrame.describe() Function

Publish Date:2025/05/01 Views:120 Category:Python

Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial