python - 在 Polars Python API 中将两列组合成元组

标签 python data-manipulation python-polars

我有一个极坐标数据框,如下所示:

df = pl.DataFrame({"bid": [1, 2, 3], "fid": [4, 5, 6]})

我想将两列按行组合成一个元组,以便结果如下:

pl.DataFrame({"bfid": [(1, 4), (2, 5), (3, 6)]})

我尝试这样做:df2.with_columns(pl.map(['bid', 'fid'], lambda x: (x[0], x[1])))这是错误的,但如果我尝试扩展到大型数据集,速度也会相当慢。

是否有更好的方法来进行此类数据操作?最终结果应该是:

enter image description here

最佳答案

因此,在极坐标中组合数据帧的行列非常简单,因为这种功能已经内置。

df.select(pl.concat_list(pl.col(["bid", "fid"])).alias("bfid"))


shape: (3, 1)
┌───────────┐
│ bfid      │
│ ---       │
│ list[i64] │
╞═══════════╡
│ [1, 4]    │
├╌╌╌╌╌╌╌╌╌╌╌┤
│ [2, 5]    │
├╌╌╌╌╌╌╌╌╌╌╌┤
│ [3, 6]    │
└───────────┘

如果您想了解有关极坐标中的行式和列表计算的更多信息,user-guide 中有一个精彩的部分。

关于python - 在 Polars Python API 中将两列组合成元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74130719/

相关文章:

python - Polars 将 pl.Object 转换为 pl.Utf8 : polars. 异常。ComputeError:无法转换 'Object' 类型

python - 是否可以在 cython 中访问 Polars 的底层数据?

python - Tweepy - 获取转发推文的 ID

python - 如何在Python中获取列表中的第n block 项目?

gnuplot - 需要在 gnuplot 中绘制每第 n 行

r - 在 R 中创建/填充空数据框

python - PyQt5动态添加矩形到QML网格

python - 如何为 python 列表/集设置最大长度?

python - Pandas:将数据帧的内容合并到单个列中(作为 dict/json 列表)

dataframe - (Polars)如何通过在另一列中指定的索引从列表中获取元素