All the other answers I could find specifically referred to aggregating across all of the nested lists within a list of lists, where as I'm looking to aggregate separately for each list.
I currently have a list of lists:
master_list = [[a,a,b,b,b,c,c,c], [d,d,d,a,a,a,c,c,c], [c,c,c,a,a,f,f,f]]
I want to return a dictionary or Counter() objects for each list with a loop:
counter1 = {'a': 2, 'b': 3, 'c': 3}
counter2 = {'d': 3, 'a': 3, 'c': 3}
counter3 = {'c': 3, 'a': 2, 'f': 3}
Currently, I'm returning something that looks like this using a loop - it's not exactly what I want as it's all lumped together and I'm having trouble accessing the counter objects separately:
Input:
count = Counter()
for lists in master_list:
for words in lists:
count[words] += 1
Output:
Counter({'a': 2, 'b': 3, 'c': 3})
Counter({'d': 3, 'a': 3, 'c': 3})
Counter({'c': 3, 'a': 2, 'f': 3})
The problem with the above is that I can't seem to figure out a way to grab each Counter individually, because I'm trying to create a pandas dataframe for each one of these dictionaries/counter objects. I'm trying to do this programmatically because my "master_list" has hundreds of lists within it and I want to return a dataframe that shows the frequency of the elements for each separate list. In the end I would have a separate dataframe and Counter object for every list within "master-list"
Currently I have something that returns only 1 dataframe:
Input:
table = pandas.DataFrame(count.items())
table.columns = ['Word', 'Frequency']
table.sort_values(by=['Frequency'], ascending = [False])
Output:
Word Frequency
the 542
and 125
or 45
. .
. .
. .
. .
Any insight would be appreciated - also, any tips on handling Counter() objects seperately would be appreciated.