Код: Выделить всё
import polars as pl
df = pl.DataFrame({"col1" : ["abc", "def", "ghi"], "col2": [[1, 2, 3, 4], [1, 2], [9, 10]], "col3": [[1,4], [2,2], [3,4,5]]})
df
Пока что я мог решить эту проблему только с помощью медленного метода map_rows. >
Код: Выделить всё
def comm(lst1, lst2):
s = set(lst1).intersection(set(lst2))
s = [str(t) for t in s]
return '|'.join(s)
res = df.map_rows(lambda t: (t[0], t[1], t[2], comm(t[1], t[2])) )
res.columns = ['col1', 'col2', 'col3', 'commom']
res
Код: Выделить всё
shape: (3, 4)
┌──────┬─────────────┬───────────┬────────┐
│ col1 ┆ col2 ┆ col3 ┆ commom │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ list[i64] ┆ list[i64] ┆ str │
╞══════╪═════════════╪═══════════╪════════╡
│ abc ┆ [1, 2, … 4] ┆ [1, 4] ┆ 1|4 │
│ def ┆ [1, 2] ┆ [2, 2] ┆ 2 │
│ ghi ┆ [9, 10] ┆ [3, 4, 5] ┆ │
└──────┴─────────────┴───────────┴────────┘
TIA
Подробнее здесь: https://stackoverflow.com/questions/730 ... wo-columns