从列表的dict创建直方图

时间:2015-01-25 00:32:49

标签: python list python-2.7 dictionary histogram

我试图绘制字典中某些键(标题为' hat1'至' hat10')的数字1,2和3的频率。将我的数据(如下所示)转换为我可以绘制的格式。

data = {'hat9': [[1, 2, 3, 1, 2]], 'hat8': [[1, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]], 'hat1': [[1, 2, 3]], 'hat3': [[1, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1]], 'hat2': [[1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 'hat5': [[1, 2, 3, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 1, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 2, 3, 1, 3, 3, 3, 3]], 'hat4': [[1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 3, 1, 2, 1, 3, 2, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1]], 'hat7': [[1, 2, 3, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]], 'hat6': [[1, 2, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 1, 1, 3]], 'hat10': [[1, 2, 3, 3, 3, 3, 3, 3, 1, 2, 2, 1, 2, 3, 3, 2, 3, 3, 3, 3, 3, 2, 1, 1, 3, 3, 1, 2, 2, 3, 3, 1, 3, 3, 3, 3, 3, 2, 3, 1, 3, 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 2, 1, 3, 2, 1, 3, 2, 3, 3, 1, 2, 1, 2, 3, 3, 1, 3, 2, 2, 1, 2, 3, 3, 1, 2, 3, 2, 3, 3, 1, 3, 3, 3, 3]]}

当我跑DataFrame.from_dict(data)时,我收到的输出看起来像这样:

In [100]: DataFrame.from_dict(data)
Out[100]: 
        hat1                                              hat10  \
0  [1, 2, 3]  [1, 2, 3, 3, 3, 3, 3, 3, 1, 2, 2, 1, 2, 3, 3, ...   

                                                hat2  \
0  [1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...   

                                                hat3  \
0  [1, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1, ...   

                                                hat4  \
0  [1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, ...   

                                                hat5  \
0  [1, 2, 3, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, ...   

                                                hat6  \
0  [1, 2, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, ...   

                                            hat7  \
0  [1, 2, 3, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]   

                                                hat8             hat9  
0  [1, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, ...  [1, 2, 3, 1, 2]  

我希望有人可以帮助我将数据转换为更可行的格式,可以相对轻松地转换为图形。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

试试这个:

data = {'hat9': [[1, 2, 3, 1, 2]], 'hat8': [[1, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]], 'hat1': [[1, 2, 3]], 'hat3': [[1, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1]], 'hat2': [[1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 'hat5': [[1, 2, 3, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 1, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 2, 3, 1, 3, 3, 3, 3]], 'hat4': [[1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 3, 1, 2, 1, 3, 2, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1]], 'hat7': [[1, 2, 3, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]], 'hat6': [[1, 2, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 1, 1, 3]], 'hat10': [[1, 2, 3, 3, 3, 3, 3, 3, 1, 2, 2, 1, 2, 3, 3, 2, 3, 3, 3, 3, 3, 2, 1, 1, 3, 3, 1, 2, 2, 3, 3, 1, 3, 3, 3, 3, 3, 2, 3, 1, 3, 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 2, 1, 3, 2, 1, 3, 2, 3, 3, 1, 2, 1, 2, 3, 3, 1, 3, 2, 2, 1, 2, 3, 3, 1, 2, 3, 2, 3, 3, 1, 3, 3, 3, 3]]}


keys = []
values = []
for key,value in data.iteritems():
    keys.append(key)
    a = 0
    b = 0
    c = 0
    for x in value[0]:
        if x==1: a+=1;
        elif x ==2: b+=1;
        elif x==3: c+=1;
    values.append([a,b,c])

print keys
print values

希望这会有所帮助。密钥为['hat9', 'hat8', etc.,..]values = [[freq of 1 in 'hats9', freq of 2 in 'hats9', freq of 3 in 'hats9'], [freq of 1 in 'hats8', freq of 2 in 'hats8', freq of 3 in 'hats8'],..](3个项目列表的列表)

答案 1 :(得分:1)

如果您想使用Matplotlib创建直方图,那么除了要显示每个hist的{​​{1}}方法之外,您真的不需要做更多的事情。例如,

hat

(您需要在import pylab pylab.hist(data['hat4'][0], bins=(1,2,3,4), align='left') 处索引,因为由于某种原因,每个字典值都是长度为1的列表,单个项本身就是数据值列表。

如果你需要以某种方式聚合帽子,你需要说明如何。

enter image description here

如果您愿意,可以使用pandas [0]执行相同操作:

DataFrame