简体   繁体   English

绘制分组的pandas数据帧

[英]Plotting a grouped pandas dataframe

I spent a few hours searching for an answer, but I can't seem to get one. 我花了几个小时寻找答案,但我似乎无法得到答案。

Long story short, I have a dataframe. 长话短说,我有一个数据帧。 The following code will produce the dataframe in question (albeit anonymised with random numbers): 以下代码将生成相关数据框(尽管使用随机数字匿名):

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()

The outcome should look like: 结果应该如下:

在此输入图像描述

And the second one: 第二个:

在此输入图像描述

I have been trying to plot them with a bar chart and having the Properties as the groups and the different Attributes as the bars. 我一直试图用条形图绘制它们,并将属性作为组,将不同的属性作为条形图。 Similar to this (plotted in Excel manually though). 与此类似(尽管手动绘制在Excel中)。 I would prefer to do it in the grouped datafarme, as to be able to plot with different groupings without the need to reset the index each time. 我更愿意在分组的datafarme中进行,因为能够使用不同的分组进行绘图,而无需每次都重置索引。

在此输入图像描述

I hope this is clear. 我希望这很清楚。

Any help on this is hugely appreciated. 对此有任何帮助非常感谢。

Thanks! 谢谢! :) :)

I wouldn't bother creating your groupby result (since you aren't aggregating anything). 我不打扰创建你的groupby结果(因为你没有聚合任何东西)。 This is a pivot 这是一个pivot


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

plt.tight_layout()
plt.show()

在此输入图像描述


If aggregation is required, you can still start with your bar and use pivot_table 如果需要聚集,你仍然可以开始你的bar和使用pivot_table

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

Use unstack first: 首先使用unstack

bar_grouped['number'].unstack(0).plot(kind='bar')

[out] [OUT]

在此输入图像描述

Below code will do what you trying to establish : 下面的代码将执行您尝试建立的内容:

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()

I took the solution from here and modified it to fit your need. 我从这里采取了解决方案,并根据您的需要进行了修改。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM