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?

Timus
  • 10,974
  • 5
  • 14
  • 28

1 Answers1

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")
Redox
  • 9,321
  • 5
  • 9
  • 26