英文:
How to convert Struct to Series in Polars?
问题
I have Dataframe with stuct inside after used pl.cumfold()
. How struct convert to normal series based column?
我有一个包含结构的DataFrame,在使用了`pl.cumfold()`后。如何将结构转换为基于列的普通Series?
Is it possible with polars expression or some kind of util method? I find the only way is convert to Python and then back to DF.
是否可以使用Polars表达式或某种实用方法来实现?我发现唯一的方法是将其转换为Python,然后再转换回DF。
(Note: The code part you provided is not translated as per your request.)
英文:
I have Dataframe with stuct inside after used pl.cumfold()
.
How struct convert to normal series based column?
┌───────────────────────────────────┐
│ price │
│ --- │
│ struct[2000] │
╞═══════════════════════════════════╡
│ {null,null,null,null,30302.67187… │
└───────────────────────────────────┘
Is it possible with polars expression or some kind of util method?
I find the only way is convert to Python and then back to DF.
df=pl.DataFrame(pl.Series('price', df[0, 0].values()))
答案1
得分: 1
这是transpose
一旦结构被unnest
后的操作。这可能会相对较慢,但我认为没有其他方法:
df = pl.DataFrame({"price": {'test0': None, 'test1': 1, 'test2': 2}})
df.unnest('price').transpose(column_names=['price'])
形状: (3, 1)
┌───────┐
│ price │
│ --- │
│ f64 │
╞═══════╡
│ null │
│ 1.0 │
│ 2.0 │
└───────┘
英文:
There is transpose
once the struct is unnest
ed. This will be relatively slow but I don't think there is any other way:
df = pl.DataFrame( {"price": {'test0' : None, 'test1' : 1, 'test2' : 2}})
df.unnest('price').transpose(column_names=['price'])
shape: (3, 1)
┌───────┐
│ price │
│ --- │
│ f64 │
╞═══════╡
│ null │
│ 1.0 │
│ 2.0 │
└───────┘
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论