list to columns, without `get`

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

polars: list to columns, without `get`

问题

你可以尝试使用以下代码来动态计算列表中元素的数量而不硬编码 2

import polars as pl

df = pl.DataFrame({'a': [[1,2], [3,4]]})
num_elements = df['a'].apply(lambda arr: len(arr[0]))
select_expr = [pl.col('a').arr.get(i).alias(f'a_{i}') for i in range(num_elements)]

result_df = df.select(select_expr)
result_df

这段代码将首先计算列表中元素的数量,然后使用该数量创建相应数量的列。这样,你就可以适应不同列表中元素数量的情况,而不需要硬编码特定的数量。

英文:

Say I have:

In [1]: df = pl.DataFrame({'a': [[1,2], [3,4]]})

In [2]: df
Out[2]:
shape: (2, 1)
┌───────────┐
 a         
 ---       
 list[i64] 
╞═══════════╡
 [1, 2]    
 [3, 4]    
└───────────┘

I know that all elements of 'a' are lists of the same length.

I can do:

In [10]: df.select([pl.col('a').arr.get(i).alias(f'a_{i}') for i in range(2)])
Out[10]:
shape: (2, 2)
┌─────┬─────┐
 a_0  a_1 
 ---  --- 
 i64  i64 
╞═════╪═════╡
 1    2   
 3    4   
└─────┴─────┘

but this involved hard-coding 2.

Is there a way to do this without hard-coding the 2? I may not know in advance how many elements there in the lists (I just know that they all have the same number of elements)

答案1

得分: 5

你可以转换为结构体并使用.unnest()方法:

df.with_columns(pl.col("a").list.to_struct()).unnest("a")
形状: (2, 2)
┌─────────┬─────────┐
 field_0  field_1 
 ---      ---     
 i64      i64     
╞═════════╪═════════╡
 1        2       
 3        4       
└─────────┴─────────┘
英文:

You can convert to a struct and .unnest():

df.with_columns(pl.col("a").list.to_struct()).unnest("a")
shape: (2, 2)
┌─────────┬─────────┐
│ field_0 ┆ field_1 │
│ ---     ┆ ---     │
│ i64     ┆ i64     │
╞═════════╪═════════╡
│ 1       ┆ 2       │
│ 3       ┆ 4       │
└─────────┴─────────┘

huangapple
  • 本文由 发表于 2023年5月22日 01:21:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301087.html
匿名

发表评论

匿名网友

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

确定