cebs
cebs

Reputation: 43

How do I groupby two columns and create a loop to subplots?

I have a large dataframe (df) in this strutcture:

    year        person      purchase

    2016        Peter       0
    2016        Peter       223820
    2016        Peter       0
    2017        Peter       261740
    2017        Peter       339987
    2018        Peter       200000
    2016        Carol       256400
    2017        Carol       33083820
    2017        Carol       154711
    2018        Carol       3401000
    2016        Frank       824043
    2017        Frank       300000
    2018        Frank       214416259
    2018        Frank       4268825
    2018        Frank       463080
    2016        Rita        0

To see how much each person spent per year I do groupby year and person, which gives me what I want.

code:

df1 = df.groupby(['person','year']).sum().reset_index()

How do I create a loop to create subplots for each person containing what he/she spent on purchase each year?

So a subplot for each person where x = year and y = purchase.

I've tried a lot of different things explained here but none seems to work.

Thanks!

Upvotes: 1

Views: 312

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You can either do pivot_table or groupby().sum().unstack('person') and then plot:

(df.pivot_table(index='year', 
                columns='person', 
                values='purchase', 
                aggfunc='sum')
    .plot(subplots=True)
);

Or

(df.groupby(['person','year'])['purchase']
   .sum()
   .unstack('person')
   .plot(subplots=True)
);

Output:

enter image description here

Upvotes: 1

Related Questions