Prompt engineering sucks. Break free from the endless tweaking with this revolutionary approach  - Learn more

Securing AI systems is tricky, ignoring it is risky. Discover the easiest way to secure your AI end to end  - Learn more

Back to Blog
How-To

How to Rename a Column in a DataFrame

Aporia Team Aporia Team 1 min read Sep 06, 2022

In this short how-to article, we will learn how to rename a column in Pandas and PySpark DataFrames. 

dataframe column rename python pandas pyspark

Pandas

The rename function can be used for renaming the columns.

# Rename one columns
df = df.rename(columns={"date": "purchase_date"})

# Rename multiple columns
df = df.rename(columns={"date": "purchase_date", "qty": "quantity"})

Or, using the inplace parameter:

df.rename(columns={"date": "purchase_date"}, inplace=True)

PySpark

The withColumnRenamed function is used for renaming columns in a PySpark DataFrame.

# Rename one column
df = df.withColumnRenamed("date", "purchase_date")

# Multiple columns
df = df.withColumnRenamed("date", "purchase_date").withColumnRenamed("qty", "quantity")

This question is also being asked as:

  • How to change DataFrame column names in PySpark?
  • Renaming column names in Pandas

People have also asked for:

Rate this article

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

On this page

Blog
Building a RAG app?

Consider AI Guardrails to get to production faster

Learn more
Table of Contents

Related Articles