Skip to content

Reshaping columns, rows, and arrays in a table

Spark SQL and DataFrame APIs provide DDL-style and functional operations to reshape tables: adding/dropping columns, renaming, splitting strings into multiple columns, filtering rows, and exploding array columns into multiple rows. These operations are used constantly when cleaning raw or semi-structured data during ETL/ELT transformations in Databricks notebooks.

1 · Learn the must-know

  • ALTER TABLE ... ADD COLUMNS adds new columns; ALTER TABLE ... DROP COLUMN drops one (requires Delta column mapping enabled for DROP COLUMN); withColumn() adds/replaces a column in a DataFrame, while drop() removes one.
  • Renaming uses ALTER TABLE ... RENAME COLUMN in SQL or withColumnRenamed() in DataFrames; renaming does not change underlying data, only metadata.
  • split(col, pattern) splits a string column into an array, and array elements can be pulled into separate columns via indexing (e.g., split(col,',')[0]) or getItem().
  • Filtering uses WHERE in SQL or .filter()/.where() on DataFrames; both accept boolean column expressions and can be chained for multiple conditions.
  • explode(array_col) turns each element of an array (or map) into a separate row, duplicating the other column values across rows; rows with NULL or empty arrays are dropped by explode but kept by explode_outer.
  • Column and row operations can be combined in a single SELECT/DataFrame chain, but exploding before filtering (or vice versa) changes row counts and results, so operation order matters.

2 · Check your understanding

Check this objectiveFree · always available

A data engineer has a table orders with a column items containing an array of struct values (product_id, qty) per order. The team needs one row per product per order, preserving the order_id and all other columns, for downstream aggregation. Which function should the engineer use?

Your objective map0 tried · 0 answered correctly · 33 untouched

What you have tried across Databricks DEA's objectives, not a readiness score.

Databricks Intelligence Platform6% of the exam0 of 2 tried
Data Ingestion and Loading21% of the exam0 of 7 tried
Data Transformation and Modeling22% of the exam0 of 7 tried
Working with Lakeflow Jobs16% of the exam0 of 4 tried
Implementing CI/CD10% of the exam0 of 4 tried
Troubleshooting, Monitoring, and Optimization10% of the exam0 of 5 tried
Governance and Security15% of the exam0 of 4 tried

3 · Keep going