我花了几个小时寻找答案,但似乎找不到答案。你知道吗
长话短说,我有一个数据帧。以下代码将生成有问题的数据帧(尽管使用随机数匿名):
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中手动绘制)。我更喜欢在分组数据场中这样做,这样就可以用不同的分组进行绘图,而不需要每次都重置索引。你知道吗
我希望这是清楚的。你知道吗
非常感谢您的帮助。你知道吗
谢谢!:)
首先使用^{} :
[输出]
我不会费心去创建你的
groupby
结果(因为你没有聚合任何东西)。这是一个pivot
如果需要聚合,您仍然可以从
bar
开始使用pivot_table
下面的代码将执行您试图建立的操作:
我从here获取了解决方案,并根据您的需要对其进行了修改。希望这有帮助!你知道吗
相关问题 更多 >
编程相关推荐