3

I have datafrme on which I perform groupby on two columns:

ax = df_all.groupby(['Component', 'SubComponent' ])['SubComponent'].count()

And this give me this:

--------------------------------------------------
| Component | SubComponent|                      |
--------------------------------------------------
|A          |First        |                     1|
--------------------------------------------------
|           |Second       |                    10|
--------------------------------------------------
|           |Third        |                     6|
--------------------------------------------------
|B          |First        |                     8|
--------------------------------------------------
|           |Second       |                     5|
--------------------------------------------------
|           |Third        |                    17|
--------------------------------------------------

Now I would like to make a histogram out of it. Each bar would be Component and each component would divided on SubComponents showing number of occurrence of each SubComponent with different color.

Is there easy way to accomplish it with matplotlib.pyploy ?

Thanks, Submi

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
Submi
  • 101
  • 1
  • 8

1 Answers1

3

You can use unstack with DataFrame.plot.bar:

print (ax.unstack())
SubComponent  First  Second  Third
Component                         
A                 1       4     15
B                 6       2      1

ax.unstack().plot.bar(stacked=True)

graph


print (ax.unstack(0))
Component      A  B
SubComponent       
First          1  6
Second         4  2
Third         15  1

ax.unstack(0).plot.bar(stacked=True)

graph3

Notice:

What is the difference between size and count in pandas?

Graham
  • 7,431
  • 18
  • 59
  • 84
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252