Question
Slicing multiple chunks in a polars dataframe
Consider the following dataframe.
df = pl.DataFrame(data={"col1": range(10)})
┌──────┐
│ col1 │
│ --- │
│ i64 │
╞══════╡
│ 0 │
│ 1 │
│ 2 │
│ 3 │
│ 4 │
│ 5 │
│ 6 │
│ 7 │
│ 8 │
│ 9 │
└──────┘
Let's say I have a list of tuples, where the first value represents the start index and the second value a length value (as used in pl.DataFrame.slice
). This might look like this:
slices = [(1,2), (5,3)]
Now, what's a good way to slice/extract two chunks out of df
, whereby the first slice starts in row 1 and has a length of 2, while the second chunk starts at row 5 and has a length of 3.
Here's what I am looking for:
┌──────┐
│ col1 │
│ --- │
│ i64 │
╞══════╡
│ 1 │
│ 2 │
│ 5 │
│ 6 │
│ 7 │
└──────┘