Suppose I have a pandas Series, like this:
import pandas as pd
s = pd.Series(["hello go home bye bye", "you can't always get", "what you waaaaaaant", "apple banana carrot munch 123"])
I want to create a dictionary with individual characters as keys, and their frequencies as values. Creating these dictionaries for words in the past has been easy with the help of collections.Counter:
from collections import Counter
c = Counter(word for row in s for word in row.lower().split())
However, I'm trying now to store individual characters and am having some issues with triple-nested dict comprehensions. Here's what I have:
c = Counter((letter for letter in word) for word for row in s for word in row.lower().split())
Which gives me a syntax error. How can I make the equivalent of the following for loop in one line?
d = {}
for row in s:
for word in row.lower().split():
for letter in word:
d[letter] += 1
I think you can use
Counter([j for i in s for j in i])
Counter({'a': 16, ' ': 13, 'e': 6, 'o': 6, 'n': 5, 't': 5, 'y': 5, 'h': 4, 'l': 4, 'c': 3, 'b': 3, 'u': 3, 'w': 3, 'g': 2, 'm': 2, 'p': 2, 'r': 2, "'": 1, '1': 1, '3': 1, '2': 1, 's': 1})
to get the individual character count.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With