Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get struct datatype similar to value_counts() return in polars?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading