1

I have a large data set containing years, NFL teams, their total salaries paid out for that year, and more misc stats. I want to create a plot that has the years on the x-axis, total salaries on the y and then has multiple lines, one for each team.

The data I want to plot looks something like this except there are of course way more teams and years and the total salaries are accurate:

Year Team Salaries
2015 Miami $100
2015 Denver $150
2015 LA $125
2016 Miami $125
2016 Denver $100
2016 LA $100

I know pandas plot function and I can set the x-axis but when I set y to be total salaries it just gives me a single line. I also do not know how to set it to break up the data by each team so each team is treated as a separate line.

2

2 Answers 2

5

You want to use a pivot table to get a new column per team.

Once you've got the data reshaped like this, plotting is easy. Check out the documentation on pivot tables.

import pandas as pd
df = pd.DataFrame(
     {
         "Year": ["2015", "2016", "2017", "2018"] * 6,
         "Team": ["Miami", "Denver", "LA"] * 8,
         "Salaries": [100, 200, 150, 125, 100, 250] * 4,
     }
)

df.pivot_table(values="Salaries",index="Year",columns="Team").plot()

The result of the pivot table looks like this

Team    Denver  LA  Miami
Year            
2015    100 150 100
2016    200 250 125
2017    100 150 100
2018    200 250 125

And the plot: enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

hi @ChristianFett, another way of thanking here is accepting the answer to signal others that the issue is resolved.
4

Alternative via seaborn:

import seaborn as sns

import pandas as pd

df = pd.DataFrame(
    {
        "Year": ["2015", "2016", "2017", "2018"] * 6,
        "Team": ["Miami", "Denver", "LA"] * 8,
        "Salaries": [100, 200, 150, 125, 100, 250] * 4,
    }
)


sns.lineplot(x='Year', y='Salaries', hue='Team', data=df)

OUTPUT:

enter image description here

NOTE: Thanks to @Cornelius Roemer for the model data.

2 Comments

Ah nice that Seaborn has the pivot built in! You stole my model data though :p
@CorneliusRoemer yup hue parameter is quite handy. Thanks for the model data : )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.