列表清单中的DataFrame [英] DataFrame from list of list

查看:128
本文介绍了列表清单中的DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有列表列表

u=[[1, 1], [2, 1, 1, 1], [2, 2, 1, 1, 1, 1, 2, 2], [2, 2, 2, 2, 2, 3, 2, 2, 1, 1, 1, 1, 1, 2, 2, 2], [2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 3, 2, 3, 3, 3, 2, 2, 3, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2]]

我想使用pandas创建一个DataFrame,其中行以u的长度为索引,而列由此列表内的一组数字给出.

I want to create a DataFrame using pandas where the rows are indexed by the length of u and the columns are given by the group of numbers inside this list of list.

我希望此DataFrame的元素成为出现这些元素的频率.例如,从上面我想得到下表

I want the element of this DataFrame to be the frequency in which the elements occurs. For example, from above, I want to get the following table

在上面的表中,带有1的列给出了每个列表中的1的数目,而2给出了2的数目.在单元格(1,1)中,通过对第一个列表中的1的数目进行计数来获得2是[1,1].在单元格(2,1)中,通过对列表[2,1,1,1]中的个数进行计数来获得数字3,而在单元格(2,2)中,通过对两个频率进行计数来获得数字2.在列表[2,1,1,1]中,始终重复相同的过程.

In the Table above the column with 1 gives the number of ones in each list while 2 gives a number of 2. In cell (1,1) the number 2 was obtained by counting the number of ones in the first list that is [1,1]. In cell (2,1) the number 3 was obtained by counting a number of ones in the list [2,1,1,1] while in the cell (2,2) the number two was obtained by counting the frequency of two in the list [2,1,1,1] the same procedure was repeated throughout.

我知道要计算列表中重复元素的数量,我必须使用count.例如 [1,1,1,2] .count(1)= 3 我想知道的是使用Pandas,以便如上所述获得DataFrame.可以这样做吗?

I know that to count number of repeating elements in a list I have to use count. for example [1,1,1,2].count(1)=3 what I want to know is to use Pandas so that I get the DataFrame as above. Is it possible to do this?

推荐答案

您可以使用Counter来将列表转换为字典.然后使用pd.DataFrame转换该词典

You can use Counter in order to transform list in to dictionary. Then using pd.DataFrame to convert that dictionary

import pandas as pd
from collections import Counter
df = pd.DataFrame([Counter(u_) for u_ in u]).fillna(0)

请注意,此处没有4,您可以将其手动添加到字典中,也可以在数据框中添加4列,即df[4] = 0

note that there is no 4 in here, you can manually add it to dictionary or just add the 4 column in dataframe after i.e. df[4] = 0

这篇关于列表清单中的DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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