I have a polars dataframe that looks like:
df = pl.DataFrame({"bid": [1, 2, 3], "fid": [4, 5, 6]})
I would like to combine the two columns row wise into a tuple so that the result looks like:
pl.DataFrame({"bfid": [(1, 4), (2, 5), (3, 6)]})
I tried to do this: df2.with_columns(pl.map(['bid', 'fid'], lambda x: (x[0], x[1])))
which is wrong but also quite slow if I try to scale up to large datasets.
Is there a better way to do this type of data manipulation? End result should be:
So to combine row-wise columns of a dataframe in polars is pretty straight forward since this kind of functionality is already built in.
df.select(pl.concat_list("bid", "fid").alias("bfid"))
shape: (3, 1)
┌───────────┐
│ bfid │
│ --- │
│ list[i64] │
╞═══════════╡
│ [1, 4] │
│ [2, 5] │
│ [3, 6] │
└───────────┘
If you want to know more about row-wise and list computation in polars, there is a wonderful section in the user-guide
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