2

Say I have:

In [1]: df = pl.DataFrame({'a': [[1,2], [3,4]]})

In [2]: df
Out[2]:
shape: (2, 1)
┌───────────┐
│ a         │
│ ---       │
│ list[i64] │
╞═══════════╡
│ [1, 2]    │
│ [3, 4]    │
└───────────┘

I know that all elements of 'a' are lists of the same length.

I can do:

In [10]: df.select([pl.col('a').arr.get(i).alias(f'a_{i}') for i in range(2)])
Out[10]:
shape: (2, 2)
┌─────┬─────┐
│ a_0 ┆ a_1 │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1   ┆ 2   │
│ 3   ┆ 4   │
└─────┴─────┘

but this involved hard-coding 2.

Is there a way to do this without hard-coding the 2? I may not know in advance how many elements there in the lists (I just know that they all have the same number of elements)

ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65

1 Answers1

3

You can convert to a struct and .unnest():

df.with_columns(pl.col("a").list.to_struct()).unnest("a")
shape: (2, 2)
┌─────────┬─────────┐
│ field_0 ┆ field_1 │
│ ---     ┆ ---     │
│ i64     ┆ i64     │
╞═════════╪═════════╡
│ 1       ┆ 2       │
│ 3       ┆ 4       │
└─────────┴─────────┘
Banana
  • 1,149
  • 7
  • 24
jqurious
  • 9,953
  • 1
  • 4
  • 14