
Answer-first summary for fast verification
Answer: storesDF first() "details" "sqft"
In PySpark, storesDF.first() returns a Row object. Accessing a struct column from a Row requires dictionary-style indexing: `row["details"]` which returns another Row object. You then index into that Row again to get the nested field: `row["details"]["sqft"]` "details.sqft" does not work directly on a Row object — dot notation is for column expressions, not Row access. Wrapping in int() ensures the result is a Python int rather than a PySpark long. Final Code: `int(storesDF.first()["details"]["sqft"])`
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
You are given the following PySpark DataFrame storesDF:
from pyspark.sql import Row
data = [
Row(id=1, name="Store A", details=Row(sqft=1200, street="Main Street")),
Row(id=2, name="Store B", details=Row(sqft=1500, street="Broadway"))
]
storesDF = spark.createDataFrame(data)
storesDF.printSchema()
from pyspark.sql import Row
data = [
Row(id=1, name="Store A", details=Row(sqft=1200, street="Main Street")),
Row(id=2, name="Store B", details=Row(sqft=1500, street="Broadway"))
]
storesDF = spark.createDataFrame(data)
storesDF.printSchema()
Schema:
root
|-- id: long (nullable = true)
|-- name: string (nullable = true)
|-- details: struct (nullable = true)
| |-- sqft: long (nullable = true)
| |-- street: string (nullable = true)
root
|-- id: long (nullable = true)
|-- name: string (nullable = true)
|-- details: struct (nullable = true)
| |-- sqft: long (nullable = true)
| |-- street: string (nullable = true)
You need to extract the sqft value from the details struct of the first row as a Python int.
Fill in the blanks in the following code:
int(__1__.__2__[__3__][__4__])
A
storesDF first "details" "sqft"
B
storesDF first() "details" "sqft"
C
storesDF head "details.sqft" None
D
storesDF first() "details.sqft" None