为列表列表中的每个列表创建一个单独的Counter()对象和Pandas DataFrame [英] Creating a separate Counter() object and Pandas DataFrame for each list within a list of lists

查看:175
本文介绍了为列表列表中的每个列表创建一个单独的Counter()对象和Pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以找到的所有其他答案具体涉及到列表列表中的所有嵌套列表的聚合,我正在寻找为每个列表单独聚合。


$ b $我目前有一个列表:

  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]] 

我想用一个循环为每个列表返回一个字典或Counter()对象:

  counter1 = {'a':2,'b':3,'c':3} 
counter2 = {'d':3, 'a':3,'c':3}
counter3 = {'c':3,'a':2,'f':3}
pre>

目前,我正在使用循环返回看起来像这样的东西 - 这不是我想要的,因为它们都集中在一起,我无法访问计数器对象分开:

 输入:

count = Counter()
master_list:
列表中的单词:
计数[单词] + = 1


输出:

计数器({'a':2,'b':3,'c':3} )
计数器({'d':3,'a':3,'c':3})
计数器({'c':3,'a':2,'f' 3})

上面的问题是,我似乎无法找出一种方法每个计数器单独抓取,因为我正在为每个这些字典/计数器对象创建一个熊猫数据框。我试图以编程方式执行此操作,因为我的master_list中有数百个列表,我想返回一个数据框,显示每个单独列表的元素的频率。最后,我将为主列表中的每个列表分别设置一个数据框和Counter对象



目前我有一些只返回1个数据框的东西:

 输入:

表= pandas.DataFrame(count.items())
table.columns = ['Word','Frequency']
table.sort_values(by = ['Frequency'],ascending = [False])


输出:

字频率
542
和125
或45
。 。
。 。
。 。
。 。

任何见解都将不胜感激 - 同样,任何有关处理Counter()对象的提示都会受到赞赏。

解决方案

您可以创建列表并附加计数器。 (另外,您使用计数器,但仍然自动计数,这是不必要的。)

  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]] 
counters = []
在master_list中的list_:
counters.append(Counter(list_))

现在,您可以使用 counters [i]


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.

解决方案

You can create a list and append the counters to it. (Also, you are using Counter, but still doing the counts yourself, which is unnecessary.)

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]]
counters = []
for list_ in master_list:
    counters.append(Counter(list_))

Now you can address each separate list with counters[i].

这篇关于为列表列表中的每个列表创建一个单独的Counter()对象和Pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆