0

I have a dataset which contains salary of different employee id in different years. The sample data looks like one below:

| Id | 2001  | 2002  | 2003  | 2004  | 2005  | 2006  |
|----|-------|-------|-------|-------|-------|-------|
| 1  | 10000 | 10586 | 11646 | 12324 | 15668 | 16447 |
| 2  | 43549 | 45234 | 51233 | 52345 | 53344 | 56544 |
| 3  | 34252 | 35563 | 36433 | 36658 | 37435 | 37799 |
| 4  | 43534 | 45473 | 47574 | 48755 | 49776 | 49876 |

I want to create a line chart in python plotly for last 4 years by writing a function so that when new data comes, I don't have to explicitly mention last 4 years. The x axis axis would be the years, y axis would be salary and different line charts would represent the Id.

1 Answer 1

1

You can set the years as index, sort by year (just to be sure) and then create a line chart using only the last four rows (years) in the dataframe (df[-4:]):

import pandas as pd
pd.options.plotting.backend = "plotly"

data= [ { "Id": 1, "2001": 10000, "2002": 10586, "2003": 11646, "2004": 12324, "2005": 15668, "2006": 16447 }, { "Id": 2, "2001": 43549, "2002": 45234, "2003": 51233, "2004": 52345, "2005": 53344, "2006": 56544 }, { "Id": 3, "2001": 34252, "2002": 35563, "2003": 36433, "2004": 36658, "2005": 37435, "2006": 37799 }, { "Id": 4, "2001": 43534, "2002": 45473, "2003": 47574, "2004": 48755, "2005": 49776, "2006": 49876 } ]
df=pd.DataFrame(data).set_index('Id')
df = df.T.sort_index()
df[-4:].plot.line()

Or if you wish to wrap it in a function:

def get_plot(dataframe):
  return dataframe.T.sort_index()[-4:].plot.line()

Result:

enter image description here

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

Comments

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.