Python – Is a dictionary slow to find frequency of each character?

Performance comparison Note: time in the table doesn’t include the time it takes to load files. | approach | american-english, | big.txt, | time w.r.t. defaultdict | | | time, seconds | time, seconds | | |—————-+——————-+—————+————————-| | Counter | 0.451 | 3.367 | 3.6 | | setdefault | 0.348 | 2.320 | 2.5 | … Read more

How to count the frequency of the elements in an unordered list?

In Python 2.7 (or newer), you can use collections.Counter: import collections a = [1,1,1,1,2,2,2,2,3,3,4,5,5] counter=collections.Counter(a) print(counter) # Counter({1: 4, 2: 4, 3: 2, 5: 2, 4: 1}) print(counter.values()) # [4, 4, 2, 1, 2] print(counter.keys()) # [1, 2, 3, 4, 5] print(counter.most_common(3)) # [(1, 4), (2, 4), (3, 2)] print(dict(counter)) # {1: 4, 2: 4, … Read more

Frequency counts in R [duplicate]

I’m not really sure what you used, but table works fine for me! Here’s a minimal reproducible example: df <- structure(list(V1 = c(“a”, “a”, “a”, “b”, “b”, “b”, “c”), V2 = c(“g”, “h”, “g”, “i”, “g”, “h”, “i”)), .Names = c(“V1”, “V2”), class = “data.frame”, row.names = c(NA, -7L)) table(df) # V2 # V1 g … Read more

Count the frequency that a value occurs in a dataframe column

Use groupby and count: In [37]: df = pd.DataFrame({‘a’:list(‘abssbab’)}) df.groupby(‘a’).count() Out[37]: a a a 2 b 3 s 2 [3 rows x 1 columns] See the online docs: https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html Also value_counts() as @DSM has commented, many ways to skin a cat here In [38]: df[‘a’].value_counts() Out[38]: b 3 a 2 s 2 dtype: int64 If … Read more