如何将宽格式的 Polars DataFrame 在 Rust 中堆叠成窄格式的 DataFrame?

huangapple go评论69阅读模式
英文:

How do I stack a wide Polars DataFrame in Rust into a narrow DataFrame?

问题

在R中,我正在像这样堆叠一个数据框:stack(preds[1:(ncol(preds)-4)])

这会选择1,000列,并将它们全部堆叠到一个单独的列中,同时创建一个第二列,该列是原始行来自的列的名称的字符串。

然后,我使用cbind()组合了我没有堆叠的其余列,由于它们的长度与堆叠的数据框不同,它们会被隐式重复。

cbind(preds[ncol(preds)], preds[ncol(preds)-3], preds[ncol(preds)-2], preds[ncol(preds)-1], stack(preds[1:(ncol(preds)-4)])

我正在尝试在Rust中使用Polars来复制这个操作,但找不到任何堆叠函数。我是否只需迭代要堆叠的所有Series,调用append(),并手动重复其余的列,然后使用with_columns()将它们连接到堆叠的列中?

英文:

In R, I am stacking a data.frame like so: stack(preds[1:(ncol(preds)-4)])

This selects 1,000 columns, and stacks them all into a single column, while creating a second column which is a string, the name of the column that row originally came from.

I then combine the remaining columns I did not stack with cbind(), which are implicitly repeated since they do not have the same length as the stacked data.frame.

cbind(preds[ncol(preds)], preds[ncol(preds)-3], preds[ncol(preds)-2], preds[ncol(preds)-1], stack(preds[1:(ncol(preds)-4)]))

I am trying to replicate this in Rust using Polars, but cannot find any kind of stacking function. Do I just need to iterate over all the Series to be stacked, calling append(), and manually repeat the remaining columns and join those to the stacked one with with_columns()?

答案1

得分: 1

melt() 是用于此目的的内置函数。请注意,当前最新版本0.27.2的时间复杂度是O(n^2)。如果您需要它更快,您将需要从主分支安装 polars = { git = "https://github.com/pola-rs/polars" },以获取最新的bug修复,使其具有O(n)的时间复杂度。

英文:

melt() is the built-in function for this purpose. Note that the current latest release, 0.27.2, is O(n^2). If you need it to be fast you will need to install from the master branch polars = { git = "https://github.com/pola-rs/polars" } to get the latest bug fix that makes it O(n).

huangapple
  • 本文由 发表于 2023年2月16日 09:44:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定