绘制分组的熊猫数据框

时间:2019-08-06 12:43:20

标签: python pandas matplotlib bar-chart

我花了几个小时寻找答案,但似乎找不到答案。

长话短说,我有一个数据框。以下代码将产生相关数据帧(尽管使用随机数进行了匿名处理):

variable1 = ["Attribute 1","Attribute 1","Attribute 1","Attribute 1","Attribute 1","Attribute 1","Attribute 2","Attribute 2",
         "Attribute 2","Attribute 2","Attribute 2","Attribute 2","Attribute 3","Attribute 3","Attribute 3","Attribute 3",
         "Attribute 3","Attribute 3","Attribute 4","Attribute 4","Attribute 4","Attribute 4","Attribute 4","Attribute 4",
         "Attribute 5","Attribute 5","Attribute 5","Attribute 5","Attribute 5","Attribute 5"]


variable2 = ["Property1","Property2","Property3","Property4","Property5","Property6","Property1","Property2","Property3",
         "Property4","Property5","Property6","Property1","Property2","Property3",
         "Property4","Property5","Property6","Property1","Property2","Property3","Property4",
         "Property5","Property6","Property1","Property2","Property3","Property4","Property5","Property6"]

number = [93,224,192,253,186,266,296,100,135,169,373,108,211,194,164,375,211,71,120,334,59,164,348,50,249,18,251,343,172,41]

bar = pd.DataFrame({"variable1":variable1, "variable2":variable2, "number":number})

bar_grouped = bar.groupby(["variable1","variable2"]).sum()

结果应如下所示:

enter image description here

第二个:

enter image description here

我一直在尝试用条形图来绘制它们,并将“属性”作为组,将不同的“属性”作为条形。与此类似(尽管在Excel中手动绘制)。我希望在分组数据场中进行此操作,以便能够使用不同的分组进行绘制,而不必每次都重新设置索引。

enter image description here

我希望这很清楚。

对此的任何帮助都将受到高度赞赏。

谢谢! :)

3 个答案:

答案 0 :(得分:4)

我不会费心创建groupby结果(因为您没有汇总任何内容)。这是pivot


bar.pivot('variable2', 'variable1', 'number').plot(kind='bar')

plt.tight_layout()
plt.show()

enter image description here


如果需要汇总 ,您仍然可以从bar开始使用pivot_table

bar.pivot_table(index='variable2', columns='variable1', values='number', aggfunc='sum')

答案 1 :(得分:3)

首先使用unstack

{{1}}

[出]

enter image description here

答案 2 :(得分:2)

下面的代码将完成您尝试建立的工作:

import numpy as np
import matplotlib.pyplot as plt

# set width of bar
barWidth = 0.25
f = plt.figure(figsize=(15,8))

bars={}
bar_pos={}
for i,proprty in enumerate(bar_grouped.unstack().columns.droplevel(0).tolist()):
    bars[i] = bar_grouped.unstack()['number',proprty].tolist()
    if(i==0):
        bar_pos[i]=2*np.arange(len(bars1))
    else:
        bar_pos[i]=[x + barWidth for x in bar_pos[i-1]] 
    plt.bar(bar_pos[i], bars[i], width=barWidth, edgecolor='white', label=proprty, figure=f)

# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([2*r + 2*barWidth for r in range(len(bars[0]))], bar_grouped.unstack().index.tolist())
# plt.figure(figsize=(10,5))

# Create legend & Show graphic
plt.legend(loc=0)
plt.show()

我从here那里获得了解决方案,并对其进行了修改以满足您的需求。希望这会有所帮助!