QuestionQ238

VerifiedData Processing & Transformations

A data engineer has a PySpark DataFrame, events_df, that contains a nested device struct, which itself contains a nested location struct, as shown:

Question Image

The objective is to flatten the nested fields into root-level columns while retaining the event identifiers and timestamp.

Which PySpark expression accomplishes this?

Explanation

DataFrame.select projects the specified columns into the resulting DataFrame, and dotted field paths select fields within nested StructType columns. Selecting device.id, device.model, device.location.latitude, and device.location.longitude therefore extracts every nested leaf value as a root-level column while also retaining event_id and event_ts.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!