如何使用Pandas Python绘制堆积的条形图 [英] How to plot a stacked bar chart using pandas python

查看:70
本文介绍了如何使用Pandas Python绘制堆积的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个用于年度数据的数据框(分别用于2014、2015和2016),每个数据框都有3列,分别为'PRACTICE','BNF NAME','ITEMS'.

I have 3 dataframes for yearly data (one for 2014, 2015 and 2016), each having 3 columns named, 'PRACTICE', 'BNF NAME', 'ITEMS'.

BNF NAME是指药物名称,我正在挑选3种氨苄青霉素,阿莫西林和Co-Amoxiclav.我想忽略此栏的不同强度/剂量(例如200克阿莫西拉夫或300毫克阿莫西拉夫等),因此我使用了 str.contains()选择这3种药物. ITEMS 是每种药物的处方总数.

BNF NAME refers to drug names and I am picking out 3 Ampicillin, Amoxicillin and Co-Amoxiclav. This column has different strengths/dosages (e.g Co-Amoxiclav 200mg or Co-Amoxiclav 300mg etc etc) that I want to ignore, so I have used str.contains() to select these 3 drugs. ITEMS is the total number of prescriptions written for each drug.

我想创建一个堆积的条形图,其中x轴为年份(2014、2014、2015),y轴为处方总数,并将3种条形中的每种条形分为每种药物名称的3种

I want to create a stacked bar chart with the x axis being year (2014, 2014, 2015) and the y axis being total number of prescriptions, and each of the 3 bars to be split up into 3 for each drug name.

我假设我需要使用 df.groupby()并选择一个部分字符串,但是我不确定如何组合年度数据,然后如何将数据分组以创建堆叠式条形图.

I am assuming I need to use df.groupby() and select a partial string maybe, however I am unsure how to combine the yearly data and then how to group the data to create the stacked bar chart.

任何指导将不胜感激.

这是我用来选择3种药品名称的行的代码行.

This is the line of code I am using to select the rows for the 3 drug names only.

frame=frame[frame['BNF NAME'].str.contains('Ampicillin' and 'Amoxicillin' and 'Co-Amoxiclav')] 

这是每个数据框的相似之处:

This is what each of the dataframes resembles:

PRACTICE |  BNF NAME |  ITEMS
Y00327 | Co-Amoxiclav_Tab 250mg/125mg | 23
Y00327 | Co-Amoxiclav_Susp 125mg/31mg/5ml S/F | 10
Y00327 | Co-Amoxiclav_Susp 250mg/62mg/5ml S/F | 6
Y00327 | Co-Amoxiclav_Susp 250mg/62mg/5ml | 1
Y00327 | Co-Amoxiclav_Tab 500mg/125mg | 50

推荐答案

您可能会通过几种不同的方式来完成此任务.这就是我要怎么做.我使用的是Jupyter笔记本,因此您的matplotlib导入可能会有所不同.

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.pyplot as 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]:
BNF NAME    ITEMS   PRACTICE
0   Co-Amoxiclav_Tab 250mg/125mg    23  Y00327
1   Co-Amoxiclav_Susp 125mg/31mg/5ml S/F    10  Y00327
2   Co-Amoxiclav_Susp 250mg/62mg/5ml S/F    6   Y00327
3   Ampicillin 250mg/62mg/5ml   1   Y00327
4   Amoxicillin_Tab 500mg/125mg 50  Y00327

模拟三个数据框:

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

结合三个数据框:

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)

这篇关于如何使用Pandas Python绘制堆积的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆