Access a field by name
name of the field
pl.DataFrame({
objs: [
{ a: 1, b: 2.0, c: "abc" },
{ a: 10, b: 20.0, c: "def" },
],
}).select(
col("objs").struct.field("b"),
col("objs").struct.field("c").as("last")
);
>>shape: (2, 2)
┌──────┬──────┐
│ b ┆ last │
│ --- ┆ --- │
│ f64 ┆ str │
╞══════╪══════╡
│ 2.0 ┆ abc │
│ 20.0 ┆ def │
└──────┴──────┘
Convert this struct to a string column with json values.
pl.DataFrame( {"a": [{"a": [1, 2], "b": [45]}, {"a": [9, 1, 3], "b": null}]}
).withColumn(pl.col("a").struct.jsonEncode().alias("encoded"))
shape: (2, 2)
┌──────────────────┬────────────────────────┐
│ a ┆ encoded │
│ --- ┆ --- │
│ struct[2] ┆ str │
╞══════════════════╪════════════════════════╡
│ {[1, 2],[45]} ┆ {"a":[1,2],"b":[45]} │
│ {[9, 1, 3],null} ┆ {"a":[9,1,3],"b":null} │
└──────────────────┴────────────────────────┘
Access a field by index (zero based index)
index of the field (starts at 0)
pl.DataFrame({
objs: [
{ a: 1, b: 2.0, c: "abc" },
{ a: 10, b: 20.0, c: "def" },
],
}).select(
col("objs").struct.nth(1),
col("objs").struct.nth(2).as("last"),
);
>>shape: (2, 2)
┌──────┬──────┐
│ b ┆ last │
│ --- ┆ --- │
│ f64 ┆ str │
╞══════╪══════╡
│ 2.0 ┆ abc │
│ 20.0 ┆ def │
└──────┴──────┘
Rename the fields of a struct
new names of the fields
pl.DataFrame({
objs: [
{ a: 1, b: 2.0, c: "abc" },
{ a: 10, b: 20.0, c: "def" },
]}).select(col("objs").struct.renameFields(["a", "b", "c"]));
>>shape: (2, 1)
┌───────────────────┐
│ objs │
│ --- │
│ struct[3] │
╞═══════════════════╡
│ {1.0,2.0,"abc"} │
│ {10.0,20.0,"def"} │
└───────────────────┘
Add/replace fields in a struct
pl.DataFrame({
objs: [
{ a: 1, b: 2.0, c: "abc" },
{ a: 10, b: 20.0, c: "def" },
],
more: ["text1", "text2"],
final: [100, null],
}).select(
col("objs").struct.withFields([
pl.lit(null).alias("d"),
pl.lit("text").alias("e"),
]),
col("objs")
.struct.withFields([col("more"), col("final")])
.alias("new"),
);
shape: (2, 2)
┌───────────────────────────────┬────────────────────────────────┐
│ objs ┆ new │
│ --- ┆ --- │
│ struct[5] ┆ struct[5] │
╞═══════════════════════════════╪════════════════════════════════╡
│ {1.0,2.0,"abc",null,"text"} ┆ {1.0,2.0,"abc","text1",100.0} │
│ {10.0,20.0,"def",null,"text"} ┆ {10.0,20.0,"def","text2",null} │
└───────────────────────────────┴────────────────────────────────┘
Struct functions for Lazy datatframes