【发布时间】:2019-12-14 00:37:34
【问题描述】:
我花了几个小时寻找答案,但似乎找不到答案。
长话短说,我有一个数据框。以下代码将生成有问题的数据帧(尽管使用随机数匿名):
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()
结果应该是这样的:
第二个:
我一直在尝试用条形图绘制它们,并将属性作为组,将不同的属性作为条形。与此类似(尽管在 Excel 中手动绘制)。我更喜欢在分组数据农场中进行,以便能够使用不同的分组进行绘图,而无需每次都重置索引。
我希望这很清楚。
非常感谢您对此的任何帮助。
谢谢! :)
【问题讨论】:
-
试试
bar_grouped['number'].unstack(0).plot(kind='bar')
标签: python pandas matplotlib bar-chart