beginner

Selecting Columns

8 min readLast updated: 2026-07-08

Overview

Learn how to retrieve specific columns, rename column headers with aliases, and drop unwanted fields from your Spark DataFrames.

What You Will Learn

In this lesson, you will learn:
  • Select Columns: Retrieving subset fields (select()).
  • Column Aliasing: Renaming columns using alias().
  • Drop Columns: Removing columns to optimize memory (drop()).

Detailed Concept Explanation

Spark DataFrames provide the .select() transformation to specify which columns should be kept or calculated. To rename columns in the output, we use .alias(). To remove columns, we use .drop().

Selecting only the columns you need reduces the volume of data that Spark needs to pass between nodes, optimizing performance.


Code Examples

Input Dataset Preview

Below is the employee dataset:

first_namelast_nameyearly_salary
AliceSmith120000
BobJones90000

Python (PySpark) Implementation

python
from pyspark.sql import SparkSession
from pyspark.sql.functions import col

spark = SparkSession.builder.appName("SelectColumns").getOrCreate()
data = [("Alice", "Smith", 120000), ("Bob", "Jones", 90000)]
df = spark.createDataFrame(data, ["first_name", "last_name", "yearly_salary"])

# Select and alias a column, and drop last_name
selected_df = df.select(col("first_name"), col("yearly_salary").alias("salary"))
selected_df.show()

Expected Output

text
+----------+------+
|first_name|salary|
+----------+------+
|     Alice|120000|
|       Bob| 90000|
+----------+------+

Execution Plan Diagram (Python & Scala)

Execution Plan Diagram
SparkSession.builder
createDataFrame
select(first_name
yearly_salary.alias(salary))
show()

Scala Implementation

scala
import org.apache.spark.sql.SparkSession

val spark = SparkSession.builder().appName("SelectScala").getOrCreate()
import spark.implicits._

val df = Seq(("Alice", "Smith", 120000), ("Bob", "Jones", 90000))
  .toDF("first_name", "last_name", "yearly_salary")

val selected = df.select($"first_name", $"yearly_salary".as("salary"))
selected.show()

Expected Output

text
+----------+------+
|first_name|salary|
+----------+------+
|     Alice|120000|
|       Bob| 90000|
+----------+------+

SQL Implementation

sql
SELECT first_name, yearly_salary AS salary 
FROM employees;

Expected Output

Executing this SQL query returns:

first_namesalary
Alice120000
Bob90000

Common Mistakes

  • Passing String Literals to Col: Mixing string literals and col() objects incorrectly in complex selections can cause parsing errors.

Best Practices

  • Drop Unused Columns: Drop unnecessary columns early in your execution flow to minimize RAM requirements.

Interview Perspective

What is the difference between select() and drop() in Spark?

select() specifies the columns you want to keep or calculate, returning a new DataFrame with only those fields. drop() does the opposite, taking a list of columns to exclude and keeping everything else.


Interactive Challenges

Challenge 1: Rename Column (Beginner)

Which method is appended to a column reference inside select() to rename it in the output DataFrame?

Related Topics