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 create a frequency table in polars from an iterator

I am trying to create a polars dataframe which is a frequency table of words in a list of words. Something like this:

from collections import defaultdict
word_freq= defaultdict(int)
for word in list_of_words:
    word_freq[word] += 1

Except, instead of a dictionary I would like it to be a polars dataframe with two columns: word, count.

I would also like to know what the best way to convert this dict to a df (in cases where that may be needed).

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 :

There is collections.Counter which simplifies this:

from collections import Counter

words = ['foo', 'foo', 'bar', 'baz', 'baz']

counts = Counter(words)
Counter({'foo': 2, 'bar': 1, 'baz': 2})

To create a Dataframe:

pl.DataFrame(list(counts.items()), schema=['word', 'count'])
shape: (3, 2)
┌──────┬───────┐
│ word ┆ count │
│ ---  ┆ ---   │
│ str  ┆ i64   │
╞══════╪═══════╡
│ foo  ┆ 2     │
│ bar  ┆ 1     │
│ baz  ┆ 2     │
└──────┴───────┘

You could also do the counting in polars with .value_counts()

pl.Series('word', words).value_counts()
shape: (3, 2)
┌──────┬────────┐
│ word ┆ counts │
│ ---  ┆ ---    │
│ str  ┆ u32    │
╞══════╪════════╡
│ foo  ┆ 2      │
│ bar  ┆ 1      │
│ baz  ┆ 2      │
└──────┴────────┘
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