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.

  • read here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – pippo1980 Jul 04 '21 at 18:34
  • As you say, you really have two questions. You should try to solve them individually. There will be answers out there for both. Use pandas pivot to get a new column per team: https://pandas.pydata.org/docs/user_guide/reshaping.html Once you've got that, the plotting is easy. – Cornelius Roemer Jul 04 '21 at 18:35

2 Answers2

3

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

Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55
  • 3
    hi @ChristianFett, another way of thanking here is [accepting](https://meta.stackexchange.com/a/5235/791774) the answer to signal others that the issue is resolved. – Mustafa Aydın Jul 04 '21 at 19:21
2

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.

Nk03
  • 14,699
  • 2
  • 8
  • 22