You can plot the filtered results of a Pandas DataFrame using the plot() method provided by Pandas. Here's an example:
import pandas as pdimport matplotlib.pyplot as plt# create a DataFramedf = pd.DataFrame({'year': [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017], 'sales': [100, 200, 150, 250, 300, 350, 400, 450]})# filter the DataFrame to include only years after 2013filtered = df[df['year'] > 2013]# plot the filtered DataFramefiltered.plot(x='year', y='sales', kind='line')plt.show() |
In this example, we first create a DataFrame df with two columns, year and sales. We then filter the DataFrame to include only rows where the year is greater than 2013, using the syntax df[df['year'] > 2013]. We assign the result to a new DataFrame called filtered.
Finally, we plot the filtered DataFrame using the plot() method with x='year' and y='sales' to specify which columns to use for the x-axis and y-axis, respectively. We also set kind='line' to create a line plot. We use plt.show() to display the plot.
You can also plot other types of plots, such as scatter plots and bar plots, by specifying the kind parameter of the plot() method. For example, to create a scatter plot of the same data, you can use:
filtered.plot(x='year', y='sales', kind='scatter')plt.show() |
Output: