martifapa
martifapa

Reputation: 117

(Python) Using groupby to iterate and plot different graphs

First of all, sorry if the title is not very representative, but I didn't really know how to put it...

I have a dataframe with the following column names:

Subject | Project | Date | Amount | Lenght | Value

And for which I want to plot several line graphs depending on certain conditions.

I know I could use df.loc to establish the conditions I want, but the combinations I need to create are just too many to hardcode (or at least I hope I'll find a way not to do it, anyway).

My intention is to plot a line graph for every possible combination of Project / Amount / Lenght, in which the x axis is the Date, the y axis is the Value, and each Subject has a different line.

There are a total of 7 subjects, 3 types of projects, 4 different amounts, and 6 different lenghts.

I have succesfully grouped all possible combinations (or at least I think I have) using:

grp = df.groupby(['Project', 'Amount', 'Length'])

for project in grp:
    # .plt()

The problem is this returns a tuple, which I can't manage to convert to df or use to plot my graphs. One of the combinations would look something like:

(('Project A', 5000, 24),      Subject Project        Date  Amount  Length    Value
0      S1     Project A  12/04/2020    5000        24  11.46
74     S2     Project A  12/04/2020    5000        24  16.39
167    S3     Project A  12/04/2020    5000        24   8.82
263    S4     Project A  13/04/2020    5000        24  11.46)

Note: the index number is not relevant for me.

Any idea how to go about it?

Thanks in advance!

Upvotes: 1

Views: 103

Answers (1)

jezrael
jezrael

Reputation: 862541

You can unpack tuples by:

for g, df in df.groupby(['Project', 'Amount', 'Length']):
    print (df)

In your solution is possible select second value of tuple by position:

for project in df.groupby(['Project', 'Amount', 'Length']):
    print (project[1])

Upvotes: 2

Related Questions