0

I need help with something easy that I can't make it work (I'm a beginner). I have a dataframe and want to plot a simple chart: the x-axis should display periods and the y-axis should display sales. Now I want the chart to show only a specific range of years, not all of them.

So far I've tried the following:

df.plot(x="Period", y="Quantity", kind="line") 

But I don't know how to narrow it down to specific values within that column, e.g. x="Period" should be greater than 2018?

0

1 Answer 1

0

Assuming df['Period'] is of type int, then you can filter the data that is sent to the plot using df[df['Period] >= 1990].

Your updated code would be...

df[df['Period']>=1990].plot(x="Period", y="Quantity", kind="line") 

For sake of completeness, if the period column is of type datetime, then you can use pandas pd.DatetimeIndex().year to get the year as int and filter for all years >=1990, like this...

df[pd.DatetimeIndex(df['Period']).year>=1990].plot(x="Period", y="Quantity", kind="line") 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.