如何在 Polars 中将结构体转换为 Series?

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

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 unnested. 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   │
└───────┘

huangapple
  • 本文由 发表于 2023年5月6日 23:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189734.html
匿名

发表评论

匿名网友

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

确定