Working with structs and JSON data
Struct is another nested data type in Polars. It represents a collection of columns. It helps you pack multiple columns into a struct
column on which you can apply operations. One good use case is to apply your transformation logic to the unique combinations of multiple columns.
JSON data is often stored as structs in a DataFrame. You can also work with JSON as strings. Polars has built-in methods to encode and decode JSON data between string and struct.
In this recipe, we’ll look at how to work with structs and JSON data.
Getting ready
We’ll use a Google Analytics dataset (which can be found at https://github.com/PacktPublishing/Polars-Cookbook/blob/main/data/ga_20170801.json) for this recipe. Read a JSON file into a DataFrame with the following code:
df = pl.read_json('../data/ga_20170801.json') cols = ['visitId', 'date', 'totals', 'trafficSource', 'customDimensions...