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 中尝试过这个,但没有成功。
.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 │
└─────┴─────┴─────┴────────────┴─────┴─────┘