Polars DataFrame 数据透视表以 List[str] 作为数据类型

问题描述 投票:0回答:1
data = {"error":[["x","z"],None,["x","z"],None],
          "X" : ["x","p","x","p"],
          "Y" : ["y","q","y","q"],
          "Z": ["z","r","z","r"],
          "time": ["Jan","Jan","Feb","Feb"],
          "value": [10,20,15,19]}

数据的数据框

poldf = pl.DataFrame(data)
pol_piv = poldf.pivot(values="value",columns="time",index=["X","Y","Z","error"])

pivot函数抛出错误,它不支持List[str] 我也在 pandas 中尝试过这个,但没有成功。

python pandas dataframe python-polars
1个回答
0
投票

在旋转之前,您可以

.cast()
List[cat]

(df.with_columns(pl.col("error").cast(pl.List(pl.Categorical)))
   .pivot(values="value", columns="time", index=["X","Y","Z","error"])
)
shape: (2, 6)
┌─────┬─────┬─────┬────────────┬─────┬─────┐
│ X   ┆ Y   ┆ Z   ┆ error      ┆ Jan ┆ Feb │
│ --- ┆ --- ┆ --- ┆ ---        ┆ --- ┆ --- │
│ str ┆ str ┆ str ┆ list[cat]  ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪════════════╪═════╪═════╡
│ x   ┆ y   ┆ z   ┆ ["x", "z"] ┆ 10  ┆ 15  │
│ p   ┆ q   ┆ r   ┆ null       ┆ 20  ┆ 19  │
└─────┴─────┴─────┴────────────┴─────┴─────┘
© www.soinside.com 2019 - 2024. All rights reserved.