我是一名有用的助手,可以为您翻译文本。
结果应该看起来像:
我花了几个小时寻找答案,但似乎找不到。
长话短说,我有一个数据框。以下代码将生成相关的数据框(尽管使用随机数字进行匿名化):
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')
。 - Chris Adams