简体   繁体   English

绘制条形图和 Groupby 关键字

[英]Plot Bar Chart and Groupby Keywords

I have a pandas data frame similar to:我有一个类似于以下内容的熊猫数据框:

Hospital                           2009-10  2010-11
Llandudno General Hospital         43       54
Dolgellau District Hospital        57       58
Deeside Community Hospital         120      140
Haverfordwest Mental Health Unit   34       30

and I want to make a bar plot of the different types of hospitals by keyword ie 'Mental Health', 'District'.我想通过关键字(即“心理健康”、“地区”)制作不同类型医院的条形图。 Grouping all the 'Mental Health' hospitals together, Grouping all the 'District' Hospitals together etc.将所有“心理健康”医院组合在一起,将所有“地区”医院组合在一起等。

Here is my code so far:到目前为止,这是我的代码:

bedsByType = df[ ['Hospital', '2009-10', '2010-11'] ].groupby(['Mental Health', 'General' , 'Community','District'])

summedAndSortedBedsByType = bedsByType.sum().sort_values( '2009-10')

summedAndSortedBedsByType.plot.barh(figsize=(25,15), title='Different Types of Hospitals')

It is not really specified in your question, how you determine your groups.在您的问题中并没有真正指定您如何确定您的组。 I assume there exists as list for the categories.我假设存在类别列表。 Then you can create your graph for instance like this:然后你可以像这样创建你的图表:

import pandas as pd
from matplotlib import pyplot as plt 

#sample df

                                Hospital  2009-10  2010-11
0             Llandudno General Hospital       43       54
1            Dolgellau District Hospital       57       58
2             Deeside Community Hospital      120      140
3       Haverfordwest Mental Health Unit       34       30
4  Morelake General Mental Health Clinic       37       39
5       Manderlay Mental Health Hospital       17       29
6             Cumbria Community Hospital       28       25
7                       Mayfair Hospital       17       19
8             New Kent District Hospital       14       17
#define categories in a list
groups = ["Mental Health", "General", "Community", "District"]
#create pattern for grouping
pattern = "|".join(groups)
#create new column with categories, if nothing applies use a fill value
df["type"] = df["Hospital"].str.extract("({})".format(pattern), expand = False).fillna("N/A")
#sum bed numbers for each category
df1 = df.groupby("type")["2009-10", "2010-11"].sum()
#create bar chart
df1.plot.barh(title = "Beds by hospital type")
plt.show()

Output:输出: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM