Код: Выделить всё
┌────────────┬─────────────────────────────────────┐
│ col1 ┆ col2 │
│ --- ┆ --- │
│ list[str] ┆ list[list[str]] │
╞════════════╪═════════════════════════════════════╡
│ ["a"] ┆ [["a"]] │
│ ["b", "c"] ┆ [["b", "c"], ["b", "c"], ["b", "c"] │
│ ["d"] ┆ [["d"]] │
└────────────┴─────────────────────────────────────┘
Код: Выделить всё
import pandas as pd
pddf = pd.DataFrame({"col1": [["a"], ["b", "c"], ["d"]],
"col2": [[["a"]], [["b", "c"], ["b", "c"], ["b", "c"]], [["d"]]]})
pddf["col2"] = pddf["col2"].apply(lambda listed: pd.DataFrame(listed).transpose().values.tolist())
print(pddf)
# col1 col2
# 0 [a] [[a]]
# 1 [b, c] [[b, b, b], [c, c, c]]
# 2 [d] [[d]]
Код: Выделить всё
import polars as pl
pldf = pl.DataFrame({"col1": [["a"], ["b", "c"], ["d"]],
"col2": [[["a"]], [["b", "c"], ["b", "c"], ["b", "c"]], [["d"]]]})
pldf = pldf.with_columns(pl.col("col2").map_elements(lambda listed: pl.DataFrame(listed).transpose().to_numpy().tolist()))
print(pldf)
# TypeError: not yet implemented: Nested object types
# Hint: Try setting `strict=False` to allow passing data with mixed types.
В более простом df pddf.transpose().values.tolist() и pldf.transpose().to_numpy().tolist() одинаковы:
Код: Выделить всё
import pandas as pd
import polars as pl
pd.DataFrame(
{"col1": ["a", "b", "c"],
"col2": ["d", "e", "f"]}
).transpose().values.tolist() == pl.DataFrame(
{"col1": ["a", "b", "c"],
"col2": ["d", "e", "f"]}
).transpose().to_numpy().tolist()
# True
(РЕДАКТИРОВАНИЕ: я немного упростил код, поскольку вторая лямбда не была действительно необходима для вопроса.)
Подробнее здесь: https://stackoverflow.com/questions/791 ... d-nested-o