Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two columns into tuple in polars python API

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:

enter image description here

like image 960
Sledge Avatar asked Oct 12 '25 04:10

Sledge


1 Answers

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

like image 133
alexp Avatar answered Oct 14 '25 16:10

alexp