我有一个熊猫数据框架,类似于:
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')发布于 2018-03-06 16:31:51
这在你的问题中并没有具体说明,你是如何决定你的组的。我想是有分类列表的吧。然后,您可以创建您的图形,例如:
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()输出:

https://stackoverflow.com/questions/49133637
复制相似问题