I have a DataFrame contains as following, where first row is the "columns":
The Pandas library provides simple and efficient tools to analyze and plot DataFrames.
Considering that the pandas library is installed and that the data are in a
.csv file (matching the example you provided).
import pandas as pd data = pd.read_csv('filename.csv')
You now have a Pandas Dataframe as follow:
id year type sale 0 1 1998 a 5 1 2 2000 b 10 2 3 1999 c 20 3 4 2001 b 15 4 5 2001 a 25 5 6 1998 b 5
This is easily achieved by:
data.plot('type', 'sale', kind='bar')
which results in
If you want the sale for each type to be summed,
data.groupby('type').sum().plot(y='sale', kind='bar') will do the trick (see #3 for explanation)
This is basically the same command, except that you have to first sum all the sale in the same year using the
groupby pandas function.
This will result in
You can also unstack the different 'type' per year for each bar by using groupby on 2 variables
data.groupby(['year', 'type']).sum().unstack().plot(y='sale', kind='bar', stacked=True)
See the Pandas Documentation on visualization for more information about achieving the layout you want.