ire
ire

Reputation: 581

Creating a stacked bar graph with varying categories

I have the following dataframe:

print(df)
                                   Keys                       Amount          Unit      Period
0                          [AWS Lambda]                 0.0988477846           USD  2021-04-01
1                  [AWS Step Functions]                     0.155275           USD  2021-04-01
2  [Amazon Simple Notification Service]                            0           USD  2021-04-01
3          [AWS Key Management Service]                 0.0028137778           USD  2021-05-01
4                          [AWS Lambda]                 0.3184488309           USD  2021-05-01
5                  [AWS Step Functions]                     0.806125           USD  2021-05-01
6                          [AWS Lambda]                 0.2898902517           USD  2021-05-25
7                  [AWS Step Functions]                     0.604075           USD  2021-05-25

I want to make a stacked bar graph, where I have the Period (dates) on the X axis and then the Amount number values for Keys in stacked bars.

The desired result is something along the lines of this: enter image description here

The problem I have is that I have varying number of groups per Period. Some categories of Keys only show up once.

Plotting with simple code like df.plot.bar(stacked=True) either leaves out categories or date points.

If possible I want to solve this while using a pandas dataframe and its native plot function but it can also be with other libraries such as matplotlib.

Is there a different approach I should take to solve this?

Upvotes: 1

Views: 323

Answers (1)

jezrael
jezrael

Reputation: 862851

Use DataFrame.explode with DataFrame.pivot_table first:

df1 = df.explode('Keys').pivot_table(index='Period', 
                                     columns='Keys', 
                                     values='Amount', 
                                     aggfunc='sum')


print (df1)
Keys        AWS Key Management Service  AWS Lambda  AWS Step Functions  \
Period                                                                   
2021-04-01                         NaN    0.098848            0.155275   
2021-05-01                    0.002814    0.318449            0.806125   
2021-05-25                         NaN    0.289890            0.604075   

Keys        Amazon Simple Notification Service  
Period                                          
2021-04-01                                 0.0  
2021-05-01                                 NaN  
2021-05-25                                 NaN  

df1.plot.bar(stacked=True)

Upvotes: 1

Related Questions