Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Groupby Plotting MultiIndex Grouped by Top Level

I am struggling to produce a pandas groupby multiindex plot the way I want. I have the following dummy pandas dataframe:

data = {
    'Day': [1, 1, 2, 2, 3, 3, 4, 2, 4],
    'Condition': ['A', 'B', 'A', 'A', 'A', 'B', 'B', 'B', 'A'],
    'Invest': [1100, 2002, 500, 200, 1030, 4000, 750, 5000, 320],
    'Spent': [100, 200, 100, 100, 100, 200, 50, 300, 250]
}

index = range(len(data['Day']))

columns = ['Day', 'Condition', 'Invest', 'Spent']

df = pd.DataFrame(data, index=index, columns=columns)

+----+-------+-------------+----------+---------+
|    |   Day | Condition   |   Invest |   Spent |
|----+-------+-------------+----------+---------|
|  0 |     1 | A           |     1100 |     100 |
|  1 |     1 | B           |     2002 |     200 |
|  2 |     2 | A           |      500 |     100 |
|  3 |     2 | A           |      200 |     100 |
|  4 |     3 | A           |     1030 |     100 |
|  5 |     3 | B           |     4000 |     200 |
|  6 |     4 | B           |      750 |      50 |
|  7 |     2 | B           |     5000 |     300 |
|  8 |     4 | A           |      320 |     250 |
+----+-------+-------------+----------+---------+

I can get the subsequent plot using:

df.groupby(['Day', 'Condition']).sum()\
   .unstack()\
   .plot(subplots=True, 
    layout=(2,2),
    figsize=(8,6));

enter image description here

Problem: I want A and B results grouped together. For example, the top plots i.e. (Invest, A) and (Invest, B) be together in one plot (similarly for Spent). Thus I would have just 2 subplots instead of 4 subplots. I have lots of example here in stackoverflow, but still could not make it work. Some suggested melting and using seaborn, still did not work and I would prefer to use pandas.

P.S.: What do I mean by "Top Level"? Whether I use the right terminology here or not, not sure, but when I unstack the groupedby pandas, in MultiIndex there are various levels, I mean to groupby the plot based on the top level as shown below:

df.groupby(['Day', 'Condition'])\
   .sum()\
   .unstack()

enter image description here

like image 562
TwinPenguins Avatar asked Nov 01 '25 16:11

TwinPenguins


1 Answers

I would do like this:

df=df.groupby(['Day', 'Condition']).sum()\
       .unstack()

df["Invest"].plot(figsize=(8,6), title="Invest")
df["Spent"].plot(figsize=(8,6), title="Spent")

plt.show()

like image 153
Klemen Koleša Avatar answered Nov 03 '25 06:11

Klemen Koleša



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!