Home > Software engineering >  Make dictionary to count words in multiple strings
Make dictionary to count words in multiple strings

Time:07-27

Working in a pandas data frame, one column is just strings and I'd like to create a single dictionary (separate from data frame) the with keys made from the words of the strings in each of the rows for that column, and the values of those keys being the count of the number of occurrences of each word/key.

I read something about the Count subclass but I'm not sure if this is the right approach. Would it be possible to use the solution in a lambda function?

CodePudding user response:

You can try:

import pandas as pd
words = pd.Series(['foo bar baz', 'foo bar amazing', 'amazing stuff is cool foo'])
words.str.split().explode().value_counts().to_dict()

Output:

{'foo': 3, 'bar': 2, 'amazing': 2, 'baz': 1, 'stuff': 1, 'is': 1, 'cool': 1}
  • Related