Rust Porars:是否可以将列表列爆炸到多个列中?
我的函数可以返回列表类型列。因此,我的专栏之一是列表。我想将此列表列转换为多列。例如:
use polars::prelude::*;
use polars::df;
fn main() {
let s0 = Series::new("a", &[1i64, 2, 3]);
let s1 = Series::new("b", &[1i64, 1, 1]);
let s2 = Series::new("c", &[Some(2i64), None, None]);
// construct a new ListChunked for a slice of Series.
let list = Series::new("foo", &[s0, s1, s2]);
// construct a few more Series.
let s0 = Series::new("Group", ["A", "B", "A"]);
let s1 = Series::new("Cost", [1, 1, 1]);
let df = DataFrame::new(vec![s0, s1, list]).unwrap();
dbg!(df);
在此阶段,DF看起来像这样:
┌───────┬──────┬─────────────────┐
│ Group ┆ Cost ┆ foo │
│ --- ┆ --- ┆ --- │
│ str ┆ i32 ┆ list [i64] │
╞═══════╪══════╪═════════════════╡
│ A ┆ 1 ┆ [1, 2, 3] │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ B ┆ 1 ┆ [1, 1, 1] │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ A ┆ 1 ┆ [2, null, null] │
问题从这里开始,我想得到:
┌───────┬──────┬─────┬──────┬──────┐
│ Group ┆ Cost ┆ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i32 ┆ i64 ┆ i64 ┆ i64 │
╞═══════╪══════╪═════╪══════╪══════╡
│ A ┆ 1 ┆ 1 ┆ 2 ┆ 3 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ B ┆ 1 ┆ 1 ┆ 1 ┆ 1 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ A ┆ 1 ┆ 2 ┆ null ┆ null │
所以我需要类似.explode()的东西,但是列的东方。是否有可能有可能的funciton?
非常感谢
I have a function which returns a list type column. Hence, one of my columns is a list. I'd like to turn this list column into multiple columns. For example:
use polars::prelude::*;
use polars::df;
fn main() {
let s0 = Series::new("a", &[1i64, 2, 3]);
let s1 = Series::new("b", &[1i64, 1, 1]);
let s2 = Series::new("c", &[Some(2i64), None, None]);
// construct a new ListChunked for a slice of Series.
let list = Series::new("foo", &[s0, s1, s2]);
// construct a few more Series.
let s0 = Series::new("Group", ["A", "B", "A"]);
let s1 = Series::new("Cost", [1, 1, 1]);
let df = DataFrame::new(vec![s0, s1, list]).unwrap();
dbg!(df);
At this stage DF looks like this:
┌───────┬──────┬─────────────────┐
│ Group ┆ Cost ┆ foo │
│ --- ┆ --- ┆ --- │
│ str ┆ i32 ┆ list [i64] │
╞═══════╪══════╪═════════════════╡
│ A ┆ 1 ┆ [1, 2, 3] │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ B ┆ 1 ┆ [1, 1, 1] │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ A ┆ 1 ┆ [2, null, null] │
Question From here, I'd like to get:
┌───────┬──────┬─────┬──────┬──────┐
│ Group ┆ Cost ┆ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i32 ┆ i64 ┆ i64 ┆ i64 │
╞═══════╪══════╪═════╪══════╪══════╡
│ A ┆ 1 ┆ 1 ┆ 2 ┆ 3 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ B ┆ 1 ┆ 1 ┆ 1 ┆ 1 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ A ┆ 1 ┆ 2 ┆ null ┆ null │
So I need something like .explode() but column-wise orient. Is there an existent funciton for this or a workaround potentially?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以。通过Polars Lazy,我们可以使用
list()
名称空间访问表达式API,以通过索引获取元素。Yes you can. Via polars lazy, we get access the to the expression API and we can use the
list()
namespace, to get elements by index.该代码在V0.27.2中的Porars的Rust V1.67上进行了测试。
另一种使用循环的方式:
This code was tested on Rust v1.67 for polars in v0.27.2.
Another way using for loop: