I have a problem of converting a struct column to a list column.
Specifically, I'm using splitn to split a string column in a dataframe by a delimiter n number of times. I want to reverse split (rsplit in Python) that string and hence want to convert it to a list so I can use .list.reverse().
Is it really correct that I cannot convert a struct to a list?
Here's an example of what I want. I have
df = data.with_columns(
pl.col("onboarded_date")
.str.splitn("/", 2)
.struct.rename_fields(["column1", "column2"])
.alias("fields")
)
That gives me a column with a struct. If I use unnest, I "expand"/flatten to separate columns. I would like to A) be able to reverse the struct before unnest'ing (which is similar to rsplit in Python) and B) explode the struct so as to generate rows instead of columns (which is possible with a list, see below):
out = data.with_columns(
pl.col("onboarded_date")
.str.split("/")
.alias("fields")
).explode("fields")
The problem of the above is that split (as opposed to splitn which I want) returns a list, not a struct.
I have tried struct.to_list() which works when its the opposite (i.e. list.to_struct). That doesn't work.
With the addition of struct unnesting at the Expression level, you can use:
.concat_list().struct.unnest()df = pl.DataFrame({
"onboarded_date": ["10/24", "11/23", "12/12"]
})
df.with_columns(
pl.concat_list(
pl.col("onboarded_date").str.splitn("/", 2).struct.unnest()
)
.alias("fields")
)
shape: (3, 2)
┌────────────────┬──────────────┐
│ onboarded_date ┆ fields │
│ --- ┆ --- │
│ str ┆ list[str] │
╞════════════════╪══════════════╡
│ 10/24 ┆ ["10", "24"] │
│ 11/23 ┆ ["11", "23"] │
│ 12/12 ┆ ["12", "12"] │
└────────────────┴──────────────┘
For reversing fields, you can just unpack them in whatever order - but it requires naming each field.
df.with_columns(
pl.col("onboarded_date").str.splitn("/", 2)
.struct.field("field_1", "field_0")
)
shape: (3, 3)
┌────────────────┬─────────┬─────────┐
│ onboarded_date ┆ field_1 ┆ field_0 │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞════════════════╪═════════╪═════════╡
│ 10/24 ┆ 24 ┆ 10 │
│ 11/23 ┆ 23 ┆ 11 │
│ 12/12 ┆ 12 ┆ 12 │
└────────────────┴─────────┴─────────┘
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With