I am trying to combine two columns row wise and get something similar to what value_counts() returns in polars.
I have tried to use the to_struct but that didn’t give the result that I expected.
data:
import polars as pl
df = pl.DataFrame({
"tags": ["c", "a", "b", "d"],
"vals":[4,3,1,1]
})
df.to_struct
<bound method DataFrame.to_struct of shape: (4, 2)
┌──────┬──────┐
│ tags ┆ vals │
│ --- ┆ --- │
│ str ┆ i64 │
╞══════╪══════╡
│ c ┆ 4 │
│ a ┆ 3 │
│ b ┆ 1 │
│ d ┆ 1 │
Expected Result:
│ struct[2] │
╞═══════════╡
│ {"c",4} │
├╌╌╌╌╌╌╌╌╌╌╌┤
│ {"a",3} │
├╌╌╌╌╌╌╌╌╌╌╌┤
│ {"b",1} │
├╌╌╌╌╌╌╌╌╌╌╌┤
│ {"d",1} │
Not sure how can I do that as I have already tried to convert into struct datatype
>Solution :
to_struct is a method that needs to be called with ().