简体   繁体   English

带有两个关键DataFrame的堆积条形图

[英]Stacked Bar Plot with Two Key DataFrame

I have a dataframe with two keys. 我有一个带有两个键的数据帧。 I'm looking to do a stacked bar plot of the number of items within key2 (meaning taking the count values from a fully populated column of data). 我正在寻找key2中项目数量的堆积条形图(意味着从完全填充的数据列中获取计数值)。

A small portion of the dataframe I have is: 我拥有的数据帧的一小部分是:

Sector            industry                   
Basic Industries  Agricultural Chemicals         17
                  Aluminum                        3
                  Containers/Packaging            1
                  Electric Utilities: Central     2
                  Engineering & Construction     12
Name: Symbol, dtype: int64

Key1 is Sector, Key2 is Industry. Key1是Sector,Key2是Industry。 I want the value in Symbol (the counted column to be represented as industry stackings) in a bar comprising Basic Industries. 我希望Symbol中的值(计数列表示为行业堆栈)在包含Basic Industries的栏中。

I know if I do a df.reset_index I'll have a column with (non-unique) Sectors and Industries with an integer counter. 我知道如果我做一个df.reset_index我会有一个带有(非唯一)Sectors和Industries的列,带有一个整数计数器。 Is there a way to simply assign the column 1,2,3 data to pandas plot or matplotlib to make a stacked bar chart? 有没有办法简单地将列1,2,3数据分配给pandas plot或matplotlib来制作堆积条形图?

Alternatively, is there a way to easily specify using both keys in the aforementioned dataframe? 或者,有没有办法在上述数据框中轻松指定使用两个键?

I'm looking for both guidance on approach from more experienced people as well as help with the actual syntax. 我正在寻找更有经验的人的方法指导以及实际语法的帮助。

I just added a new Sector to improve the example. 我刚刚添加了一个新的扇区来改进这个例子。

                                           Symbol
Sector            industry                           
Basic Industries  Agricultural Chemicals           17
                  Aluminum                          3
                  Containers/Packaging              1
                  Electric Utilities: Central       2
                  Engineering & Construction       22
Basic Industries2 Agricultural Chemicals            7
                  Aluminum                          8
                  Containers/Packaging             11
                  Electric Utilities: Central       7
                  Engineering & Construction        4

Assuming your dataframe is indexed by ["Sector", "industry"] you need first reset_index and then pivot your dataframe and finally make the stacked plot. 假设您的数据框由["Sector", "industry"]编制索引,则首先需要reset_index,然后转动数据框并最终生成堆叠图。

df.reset_index().pivot_table(index="industry", columns="Sector", values="Symbol").T.plot(kind='bar', stacked=True, figsize=(14, 6))

在此输入图像描述

另一种方法,您可以使用以下方法,而不是reset_index

df.unstack().Symbol.plot(kind='bar', stacked=True)

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

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