Skip to main content
๐Ÿ“˜ Complete PDF

PySpark Certification Practice Test 02

DataFrame Operations and Transformations

Question 1 of 20
๐Ÿ“‹ Multiple Choice
Question 21
A newly joined Data Engineer at an e-commerce company receives a customer dataset with 45 columns. Downstream reporting requires extracting only three specific columns: `customer_id`, `first_name`, and `email`. Which PySpark DataFrame transformation should be used to project this subset of columns?
A. filter()
B. groupBy()
C. dropDuplicates()
โœ“
D. select()
Answer
โœ… D. select()
Explanation

1. Concept Overview In Apache Spark, column projection is handled by the **select()** transformation. It creates a new DataFrame containing only the specified columns or expressions, pruning unused columns from downstream execution.

2. Why the Correct Option is Right **select()** is specifically engineered for column projection: `df.select("customer_id", "first_name", "email")` projects the required three columns. Spark's Catalyst Optimizer pushes this projection down into the underlying file reader (e.g., Parquet or Delta), scanning only the required physical columns from storage.

3. Why Other Options Are Incorrect - **filter():** Filters rows based on a boolean condition (row-level selection); it does not project or eliminate columns. - **groupBy():** Aggregates rows based on grouping keys; it does not perform simple column projection. - **dropDuplicates():** Eliminates duplicate rows from the DataFrame; it preserves the full set of columns.

4. Production Scenario / Code Example

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

spark = SparkSession.builder.appName("ProjectionExample").getOrCreate()

# Load customer dataset with 45 columns
df_customers = spark.read.parquet("s3://datalake/customers/")

# Project only the required columns:
df_subset = df_customers.select(
    col("customer_id"),
    col("first_name"),
    col("email")
)

# Under Parquet/Delta, only the 3 column chunks are read from disk!

5. Real-World Architecture Context Columnar storage formats (Parquet, ORC, Delta Lake) organize data by columns rather than rows. Calling `select()` early in an ETL pipeline enables **column projection pushdown**, reducing disk I/O, network transfer, and executor memory footprint by over 90% compared to scanning all 45 columns.

6. Common Pitfalls & Anti-Patterns - **Selecting columns after an expensive shuffle:** Always project columns *before* wide transformations (joins, aggregations) to avoid shuffling unneeded columns across the network. - **Passing strings with typos:** Passing non-existent column names raises an `AnalysisException`. Using `col("name")` provides cleaner syntax and supports column expressions.

7. Performance & Cost Considerations Column pruning is one of the most effective optimizations in distributed computing. Scanning 3 columns out of 45 cuts storage read requests and cluster memory consumption dramatically.

8. Exam / Interview Tip Remember: `select()` operates on **columns** (projection), while `filter()` / `where()` operates on **rows** (selection).

9. Key Takeaways - `select()` projects a subset of DataFrame columns. - Enables Catalyst column projection pushdown in columnar formats. - Always prune unneeded columns early in your pipelines.

10. Related Tutorials & Resources - [DataFrame API โ€” Select, Filter, WithColumn & Drop](/tutorials/pyspark/df-api) - [PySpark DataFrame Basics (Part 1)](/tutorials/pyspark/pyspark-dataframe-basics)`

Answered: 0 / 20
Question Navigator (Test 02)0% Answered