我有一个类似于:
的pandas数据框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
我想用关键词制作不同类型医院的条形图,即'心理健康','区'。将所有“心理健康”医院分组在一起,将所有“地区”医院分组在一起等。
到目前为止,这是我的代码:
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')
答案 0 :(得分:1)
在您的问题中,您没有真正指定,如何确定您的群组。我假设存在类别列表。然后您可以创建图形,例如:
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()