Код: Выделить всё
import polars as pl
df = pl.from_repr("""
┌──────────────┬─────────────┐
│ sub-category ┆ category │
│ --- ┆ --- │
│ str ┆ str │
╞══════════════╪═════════════╡
│ tv ┆ electronics │
│ mobile ┆ mobile │
│ tv ┆ electronics │
│ wm ┆ electronics │
│ micro ┆ kitchen │
│ wm ┆ electronics │
└──────────────┴─────────────┘
""")
Код: Выделить всё
pl.from_pandas(
df.to_pandas().apply(lambda x: x.value_counts()).reset_index()
)
Код: Выделить всё
shape: (6, 3)
┌─────────────┬──────────────┬──────────┐
│ index ┆ sub-category ┆ category │
│ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 │
╞═════════════╪══════════════╪══════════╡
│ electronics ┆ null ┆ 4.0 │
│ kitchen ┆ null ┆ 1.0 │
│ micro ┆ 1.0 ┆ null │
│ mobile ┆ 1.0 ┆ 1.0 │
│ tv ┆ 2.0 ┆ null │
│ wm ┆ 2.0 ┆ null │
└─────────────┴──────────────┴──────────┘
Подробнее здесь: https://stackoverflow.com/questions/761 ... ars-python