Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Spark Developer Associate

Columns and Rows: select, withColumn, explode

Databricks Spark Developer Associate · Lesson 8 of 18 · 1:03:13

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: select, selectExpr, withColumn, renaming and dropping columns, filter and where, split, explode, and explode_outer. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Raw events, awkward columns

Coralpeak's raw events arrive with awkward column names, a tags field that packs several labels into one string, and an items column that holds an array of purchased items. Before any report, the team must add, rename, drop, and split columns, keep the right rows, and give each item its own row. This lesson covers selecting and adding columns, renaming and dropping, filtering rows, and splitting and exploding.

Selecting and adding columns

select picks columns or expressions, and returns a new DataFrame with only those, while selectExpr takes the same expressions written as SQL strings. withColumn adds a column, or replaces it if a column with that name already exists, from an expression on the other columns. withColumns does the same for several columns at once, from a dictionary of names and expressions, which keeps the plan smaller than a long chain of withColumn calls. lit turns a constant into a column, so that you can add a fixed value, like the name of the source. And cast changes the type of a column, for example from a string to an integer or a date.

Renaming and dropping

withColumnRenamed takes the existing name and the new name, in that order, and returns a DataFrame where only that column changed. withColumnsRenamed renames several columns at once, from a dictionary of old names to new names. Both do nothing when the old name does not exist, so a typo raises no error, and the column simply keeps its name. drop removes one or more columns, and it also does nothing for a name that does not exist, while alias renames a column inside a select.

Filtering rows

filter keeps the rows where a condition is true, and the where method is simply another name for it. You combine conditions with the ampersand for and, the vertical bar for or, and the tilde for not, with parentheses around each condition, since these operators bind more tightly than comparisons in Python. Useful conditions include isin for a list of values, isNull and isNotNull, between, and like, and filter also accepts a SQL string. Rows where the condition is null, because a value is missing, are dropped as well, like the rows where it is false.

Splitting and exploding

split turns a string column into an array, with a regular expression as the separator, so a character like a pipe or a dot must be escaped. Its optional limit caps the number of elements, the last element keeping the rest of the string, and getItem, or an index, takes one element out of the array. explode turns each element of an array, or each entry of a map, into its own row, and copies the other columns. Rows where the array is null or empty produce no row at all, while explode_outer keeps them, with a null in place of the element. And posexplode also returns the position of each element, which keeps the order of the items.

Recap

Let's recap. select and selectExpr pick columns, withColumn adds or replaces one, and withColumns handles several at once. withColumnRenamed takes the old name, then the new one, and like drop, it does nothing when the name does not exist. filter, or where, keeps the rows where the condition is true, with parentheses around each condition, joined by the ampersand or the vertical bar. split turns a string into an array with a regular expression. And explode makes one row per element, and skips empty arrays, while explode_outer keeps them with a null.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. A developer calls withColumn("score", ...), and the DataFrame already has a column named score. What happens?

    • The existing score column is replaced
    • A second column named score is added
    • The call fails with an error
    • The new column is renamed score_1
    Show the answer

    The existing score column is replaced

    WithColumn replaces a column that already has the same name. “A second column named score is added” is wrong, since withColumn never adds a duplicate name. “The call fails with an error” is wrong, since replacing a column is allowed. And “The new column is renamed score_1” is wrong, since Spark does not rename the new column.

  2. Coralpeak wants the purchases above $50, from Japan or Korea. Which filter is correct?

    • df.filter(col("amount") > 50 and col("country").isin("JP", "KR"))
    • df.filter(col("amount") > 50 & col("country").isin("JP", "KR"))
    • df.filter((col("amount") > 50) & col("country").isin("JP", "KR"))
    • df.where(col("amount") > 50 | col("country").isin("JP", "KR"))
    Show the answer

    df.filter((col("amount") > 50) & col("country").isin("JP", "KR"))

    Each comparison sits in its own parentheses, and the ampersand combines them. “df.filter(col("amount") > 50 and col("country").isin("JP", "KR"))” is wrong, since the Python keyword and cannot combine column conditions, and raises an error. “df.filter(col("amount") > 50 & col("country").isin("JP", "KR"))” is wrong, since without parentheses, the ampersand applies to fifty before the comparison. And “df.where(col("amount") > 50 | col("country").isin("JP", "KR"))” is wrong, since the vertical bar means or, which would keep every purchase from Japan or Korea, at any amount.

  3. Some purchases have an empty array of items, and the report must still list them, with a null item. Which function fits?

    • explode
    • explode_outer
    • split
    • flatten
    Show the answer

    explode_outer

    Explode_outer produces one row with a null when the array is empty or null. “explode” is wrong, since explode produces no row at all for an empty array. “split” is wrong, since split builds an array from a string. And “flatten” is wrong, since flatten merges an array of arrays into one array, without creating rows.