Skip to content Skip to sidebar Skip to footer

How To Plot A Stacked Bar Chart Using Pandas Python

I have 3 dataframes for yearly data (one for 2014, 2015 and 2016), each having 3 columns named, 'PRACTICE', 'BNF NAME', 'ITEMS'. BNF NAME refers to drug names and I am picking out

Solution 1:

There are likely going to be a few different ways in which you could accomplish this. Here's how I would do it. I'm using a jupyter notebook, so your matplotlib imports may be different.

import pandas as pd
%matplotlib
import matplotlib.pyplotas plt
import matplotlib
matplotlib.style.use('ggplot')

df = pd.DataFrame({'PRACTICE': ['Y00327', 'Y00327', 'Y00327', 'Y00327', 'Y00327'],
                   'BNF NAME': ['Co-Amoxiclav_Tab 250mg/125mg', 'Co-Amoxiclav_Susp 125mg/31mg/5ml S/F',
                                'Co-Amoxiclav_Susp 250mg/62mg/5ml S/F', 'Ampicillin 250mg/62mg/5ml',
                               'Amoxicillin_Tab 500mg/125mg'],
                   'ITEMS': [23, 10, 6, 1, 50]})



Out[52]:
BNFNAMEITEMSPRACTICE0Co-Amoxiclav_Tab 250mg/125mg    23Y003271Co-Amoxiclav_Susp 125mg/31mg/5ml S/F    10Y003272Co-Amoxiclav_Susp 250mg/62mg/5ml S/F    6Y003273Ampicillin 250mg/62mg/5ml   1Y003274Amoxicillin_Tab 500mg/125mg 50Y00327

To simulate your three dataframes:

df1 = df.copy()
df2 = df.copy()
df3 = df.copy()

Set a column indicating what year the dataframe represents.

df1['YEAR'] = 2014
df2['YEAR'] = 2015
df3['YEAR'] = 2016

Combining the three dataframes:

combined_df = pd.concat([df1, df2, df3], ignore_index=True)

To set what drug each row represents:

combined_df['parsed_drug_name'] = ""# creates a blank column
amp_bool = combined_df['BNF NAME'].str.contains('Ampicillin', case=False)
combined_df.loc[amp_bool, 'parsed_drug_name'] = 'Ampicillin'# sets the row to amplicillin, if BNF NAME contains 'ampicillin.'

amox_bool = combined_df['BNF NAME'].str.contains('Amoxicillin', case=False)
combined_df.loc[amox_bool, 'parsed_drug_name'] = 'Amoxicillin'

co_amox_bool = combined_df['BNF NAME'].str.contains('Co-Amoxiclav', case=False)
combined_df.loc[co_amox_bool, 'parsed_drug_name'] = 'Co-Amoxiclav'

Finally, perform a pivot on the data, and plot the results:

combined_df.pivot_table(index='YEAR', columns='parsed_drug_name', values='ITEMS', aggfunc='sum').plot.bar(rot=0, stacked=True)

Stack Bar Plot

Post a Comment for "How To Plot A Stacked Bar Chart Using Pandas Python"