0

I'm new to PySpark and I see there are two ways to select columns in PySpark, either with ".select()" or ".withColumn()".

From what I've heard ".withColumn()" is worse for performance but otherwise than that I'm confused as to why there are two ways to do the same thing.

So when am I supposed to use ".select()" instead of ".withColumn()"?

I've googled this question but I haven't found a clear explanation.

JTD2021
  • 127
  • 2
  • 12

3 Answers3

3

Using:

df.withColumn('new', func('old'))

where func is your spark processing code, is equivalent to:

df.select('*', func('old').alias('new'))  # '*' selects all existing columns

As you see, withColumn() is very convenient to use (probably why it is available), however as you noted, there are performance implications. See this post for details: Spark DAG differs with 'withColumn' vs 'select'

bzu
  • 1,242
  • 1
  • 8
  • 14
1

@Robert Kossendey You can use select to chain multiple withColumn() statements without suffering the performance implications of using withColumn. Likewise, there are cases where you may want/need to parameterize the columns created. You could set variables for windows, conditions, values, etcetera to create your select statement.

0

.withColumn() is not for selecting columns, instead it returns a new DataFrame with a new / replaced column (docs).

Robert Kossendey
  • 6,733
  • 2
  • 12
  • 42