PySpark - rename more than one column using withColumnRenamed

Apache SparkPysparkApache Spark-SqlRename

Apache Spark Problem Overview


I want to change names of two columns using spark withColumnRenamed function. Of course, I can write:

data = sqlContext.createDataFrame([(1,2), (3,4)], ['x1', 'x2'])
data = (data
       .withColumnRenamed('x1','x3')
       .withColumnRenamed('x2', 'x4'))

but I want to do this in one step (having list/tuple of new names). Unfortunately, neither this:

data = data.withColumnRenamed(['x1', 'x2'], ['x3', 'x4'])

nor this:

data = data.withColumnRenamed(('x1', 'x2'), ('x3', 'x4'))

is working. Is it possible to do this that way?

Apache Spark Solutions


Solution 1 - Apache Spark

It is not possible to use a single withColumnRenamed call.

  • You can use DataFrame.toDF method*

      data.toDF('x3', 'x4')
    

    or

      new_names = ['x3', 'x4']
      data.toDF(*new_names)
    
  • It is also possible to rename with simple select:

      from pyspark.sql.functions import col
    
      mapping = dict(zip(['x1', 'x2'], ['x3', 'x4']))
      data.select([col(c).alias(mapping.get(c, c)) for c in data.columns])
    

Similarly in Scala you can:

  • Rename all columns:

      val newNames = Seq("x3", "x4")
    
      data.toDF(newNames: _*)
    
  • Rename from mapping with select:

      val  mapping = Map("x1" -> "x3", "x2" -> "x4")
    
      df.select(
        df.columns.map(c => df(c).alias(mapping.get(c).getOrElse(c))): _*
      )
    

    or foldLeft + withColumnRenamed

      mapping.foldLeft(data){
        case (data, (oldName, newName)) => data.withColumnRenamed(oldName, newName) 
      }
    

* Not to be confused with RDD.toDF which is not a variadic functions, and takes column names as a list,

Solution 2 - Apache Spark

I couldn't find an easy pyspark solution either, so just built my own one, similar to pandas' df.rename(columns={'old_name_1':'new_name_1', 'old_name_2':'new_name_2'}).

import pyspark.sql.functions as F

def rename_columns(df, columns):
    if isinstance(columns, dict):
        return df.select(*[F.col(col_name).alias(columns.get(col_name, col_name)) for col_name in df.columns])
    else:
        raise ValueError("'columns' should be a dict, like {'old_name_1':'new_name_1', 'old_name_2':'new_name_2'}")

So your solution will look like data = rename_columns(data, {'x1': 'x3', 'x2': 'x4'})

If you want to chain your method calls, Spark 3.0 brought in pyspark.sql.DataFrame.transform, which you can use in the following way:

my_df.transform(lambda df: rename_columns(df, {'old_name_1':'new_name_1', 'old_name_2':'new_name_2'}))

It saves me some lines of code, hope it will help you too.

Solution 3 - Apache Spark

why do you want to perform it in a single line if you print the execution plan it is actually done in single line only

data = spark.createDataFrame([(1,2), (3,4)], ['x1', 'x2'])
data = (data
   .withColumnRenamed('x1','x3')
   .withColumnRenamed('x2', 'x4'))
data.explain()

OUTPUT

== Physical Plan ==
*(1) Project [x1#1548L AS x3#1552L, x2#1549L AS x4#1555L]
+- Scan ExistingRDD[x1#1548L,x2#1549L]

if you want to do it with a tuple of list you can use a simple map function

data = spark.createDataFrame([(1,2), (3,4)], ['x1', 'x2'])
new_names = [("x1","x3"),("x2","x4")]
data = data.select(list(
       map(lambda old,new:F.col(old).alias(new),*zip(*new_names))
       ))

data.explain()

still has same plan

OUTPUT

== Physical Plan ==
*(1) Project [x1#1650L AS x3#1654L, x2#1651L AS x4#1655L]
+- Scan ExistingRDD[x1#1650L,x2#1651L]

Solution 4 - Apache Spark

This should work if you want to rename multiple columns using the same column name with a prefix

df.select([f.col(c).alias(PREFIX + c) for c in df.columns])

Solution 5 - Apache Spark

I have this hack in all of my pyspark program:

import pyspark
def rename_sdf(df, mapper={}, **kwargs_mapper):
    ''' Rename column names of a dataframe
        mapper: a dict mapping from the old column names to new names
        Usage:
            df.rename({'old_col_name': 'new_col_name', 'old_col_name2': 'new_col_name2'})
            df.rename(old_col_name=new_col_name)
    '''
    for before, after in mapper.items():
        df = df.withColumnRenamed(before, after)
    for before, after in kwargs_mapper.items():
        df = df.withColumnRenamed(before, after)
    return df
pyspark.sql.dataframe.DataFrame.rename = rename_sdf

Now you can easily rename any spark dataframe in the pandas way!

df.rename({'old1':'new1', 'old2':'new2'})

Solution 6 - Apache Spark

You can also use Dictionary to iterate through the columns you want to rename.

Sample

a_dict = {'sum_gb': 'sum_mbUsed', 'number_call': 'sum_call_date'}

for key, value in a_dict.items():
    df= df.withColumnRenamed(value,key)

Solution 7 - Apache Spark

The accepted answer by zero323 is efficient. Most of the other answers should be avoided.

Here's another efficient solution that leverages the quinn library and is well suited for production codebases:

df = spark.createDataFrame([(1,2), (3,4)], ['x1', 'x2'])
def rename_col(s):
    mapping = {'x1': 'x3', 'x2': 'x4'}
    return mapping[s]
actual_df = df.transform(quinn.with_columns_renamed(rename_col))
actual_df.show()

Here's the DataFrame that's outputted:

+---+---+
| x3| x4|
+---+---+
|  1|  2|
|  3|  4|
+---+---+

Let's take a look at the logical plans that are outputted with actual_df.explain(True) and verify they're efficient:

== Parsed Logical Plan ==
'Project ['x1 AS x3#52, 'x2 AS x4#53]
+- LogicalRDD [x1#48L, x2#49L], false

== Analyzed Logical Plan ==
x3: bigint, x4: bigint
Project [x1#48L AS x3#52L, x2#49L AS x4#53L]
+- LogicalRDD [x1#48L, x2#49L], false

== Optimized Logical Plan ==
Project [x1#48L AS x3#52L, x2#49L AS x4#53L]
+- LogicalRDD [x1#48L, x2#49L], false

== Physical Plan ==
*(1) Project [x1#48L AS x3#52L, x2#49L AS x4#53L]

The parsed logical plan and physical plan are basically equal, so Catalyst isn't doing any heavy lifting to optimize the plan.

Calling withColumnRenamed multiple times should be avoided because it creates an inefficient parsed plan that needs to be optimized.

Let's look at an unnecessarily complex parsed plan:

def rename_columns(df, columns):
    for old_name, new_name in columns.items():
        df = df.withColumnRenamed(old_name, new_name)
    return df

def rename_col(s):
    mapping = {'x1': 'x3', 'x2': 'x4'}
    return mapping[s]
actual_df = rename_columns(df, {'x1': 'x3', 'x2': 'x4'})
actual_df.explain(True)
== Parsed Logical Plan ==
Project [x3#52L, x2#49L AS x4#55L]
+- Project [x1#48L AS x3#52L, x2#49L]
   +- LogicalRDD [x1#48L, x2#49L], false

== Analyzed Logical Plan ==
x3: bigint, x4: bigint
Project [x3#52L, x2#49L AS x4#55L]
+- Project [x1#48L AS x3#52L, x2#49L]
   +- LogicalRDD [x1#48L, x2#49L], false

== Optimized Logical Plan ==
Project [x1#48L AS x3#52L, x2#49L AS x4#55L]
+- LogicalRDD [x1#48L, x2#49L], false

== Physical Plan ==
*(1) Project [x1#48L AS x3#52L, x2#49L AS x4#55L]

Solution 8 - Apache Spark

Easiest way to do this is as follows:

Explanation:

  1. Get all columns in the pyspark dataframe using df.columns
  2. Create a list looping through each column from step 1
  3. The list will output:col("col1").alias("col1_x").Do this only for the required columns
  4. *[list] will unpack the list for select statement in pypsark

from pyspark.sql import functions as F (df .select(*[F.col(c).alias(f"{c}_x") for c in df.columns]) .toPandas().head() )

Hope this helps

Solution 9 - Apache Spark

You should use this function:

def spark_rename_from_dict(df, rename_dict):
    newcols = [rename_dict.get(i,i) for i in df.columns]
    df = df.toDF(*newcols)

Here your rename dict is a mapping over a subset of df.columns. This approach is recommended since it does not create multiple dataframes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2280549View Question on Stackoverflow
Solution 1 - Apache Sparkzero323View Answer on Stackoverflow
Solution 2 - Apache SparkproggeoView Answer on Stackoverflow
Solution 3 - Apache SparkTushar KolheView Answer on Stackoverflow
Solution 4 - Apache Sparkdatamonk3yView Answer on Stackoverflow
Solution 5 - Apache SparkLouis YangView Answer on Stackoverflow
Solution 6 - Apache SparkFoxbatView Answer on Stackoverflow
Solution 7 - Apache SparkPowersView Answer on Stackoverflow
Solution 8 - Apache SparkNaveenanView Answer on Stackoverflow
Solution 9 - Apache SparkMiloMinderbinderView Answer on Stackoverflow