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

Python Polars: How does one create a cross tab table?

I would like to count unique combinations in two Polars columns.

In R

df <- data.frame(a = c(2,0,1,0,0,0), b = c(1,1,1,0,0,1))
table(df) 
    0 1
  0 2 2
  1 0 1
  2 0 1

In Pandas

import numpy as np
a = np.array([2,0,1,0,0,0])
b = np.array([1,1,1,0,0,1])
pd.crosstab(a, b)
    0   1       
0   2   2
1   0   1
2   0   1

In Polars

Is this the proper way?

df = pl.DataFrame(
    {
        "a": [2,0,1,0,0,0],
        "b": [1,1,1,0,0,1]
    }
)
df.pivot(values="a", index="b", columns="a", aggregate_function="count").fill_null(0)

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

>Solution :

I think you want to invert your "a" and "b" in your pivot. You can also use the sort_columns parameter along with a .sort at the end to get the same output

df.pivot('b', 'a','b','count',sort_columns=True).fill_null(0).sort('a')
shape: (3, 3)
┌─────┬─────┬─────┐
│ a   ┆ 0   ┆ 1   │
│ --- ┆ --- ┆ --- │
│ i64 ┆ u32 ┆ u32 │
╞═════╪═════╪═════╡
│ 0   ┆ 2   ┆ 2   │
│ 1   ┆ 0   ┆ 1   │
│ 2   ┆ 0   ┆ 1   │
└─────┴─────┴─────┘
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