不幸的是,我不得不处理极坐标数据框中的一些嵌套数据。 (我知道这是不好的做法)考虑数据:
data = {
"positions": [
{
"company": {
"companyName": "name1"
},
},
{
"company": {
"companyName": "name2"
},
},
{
"company": {
"companyName": "name3"
},
}
]
}
positions
是数据框中的一列。我已经探索了 Polars python api 文档,但无法弄清楚如何将 companyName
字段提取到单独的列表列中。
我想实现与此理解相同的效果:
names = (
[
p["company"]["companyName"]
for p in data["positions"]
if p.get("company") and p.get("company").get("companyName")
]
if data.get("positions")
else None
)
注意空检查。
我感觉我必须将
pl.list.eval
函数与 pl.element
一起使用,但我对 api 有点模糊。
Before:
shape: (3, 1)
┌─────────────┐
│ positions │
│ --- │
│ struct[1] │
╞═════════════╡
│ {{"name1"}} │
│ {{"name2"}} │
│ {{"name3"}} │
└─────────────┘
After:
shape: (3, 1)
┌───────┐
│ names │
│ --- │
│ str │
╞═══════╡
│ name1 │
│ name2 │
│ name3 │
└───────┘
.struct.field()
或 .struct[]
语法来提取结构体字段。
df.with_columns(
pl.col("positions").struct["company"].struct["companyName"]
)
shape: (3, 2)
┌─────────────┬─────────────┐
│ positions ┆ companyName │
│ --- ┆ --- │
│ struct[1] ┆ str │
╞═════════════╪═════════════╡
│ {{"name1"}} ┆ name1 │
│ {{"name2"}} ┆ name2 │
│ {{"name3"}} ┆ name3 │
└─────────────┴─────────────┘