简体   繁体   English

堆积的条形图与熊猫

[英]Stacked Bar Chart with Pandas

I'm a total beginner with programming/ Pandas, therefore, I hope this question is ok anyway. 我是编程/熊猫的初学者,因此,我希望这个问题仍然可以。 I do need to figure out how to plot basic data of my doctoral thesis in medicine. 我确实需要弄清楚如何绘制医学博士学位论文的基本数据。

What I have: 我有的:

1) column with different directions of medical specialists eg internal medicine, surgery etc. They are listed in numbers from 1-20 in that column. 1)具有不同方向的医学专家的列,例如内科,外科等。它们在该列中以1-20的数字列出。 1) = 'specalties' 2)outcome of patients at day 28: 0=dead, 1=alive, listed just as 0 and 1 in that column 2)='status_d28' 1)='特性'2)第28天的患者结果:0 =死亡,1 =存活,在该列中仅作为0和1列出2)='status_d28'

What I'd need: 我需要什么:

a stacked bar chart where 1) are listed on the x-axis and the y-axis shows the total number of patients admitted from each of those medical disciplines for those a) dead at day 28 and stacked on top b) alive at day 28. 堆叠的条形图,其中1)列在x轴上,y轴显示从这些医学学科中接受的患者总数,其中a)在第28天死亡,在b)在第28天活着。

Finally, I will need two plots: One with the total numbers of patients for y-axis as stated above and the second one with percentages of the total as y-axis, for example 20% of ALL patients alive at day 28 were admitted to general surgery, on top of that 5% of ALL patients dead at day 28 were so. 最后,我将需要两个图:一个如上所述,以y轴表示患者总数,第二个以y轴表示总数的百分比,例如,在第28天存活的所有患者中有20%一般手术,在第28天死亡的ALL患者中,有5%是如此。

I'm sorry for the very basic and unprofessional approach. 非常抱歉,这是非常基本且不专业的方法。 I'm just getting into the matter and have problems getting started at this one. 我只是开始讨论这个问题,在开始这一问题时遇到了问题。 I added an image for better understanding. 我添加了一个图像,以更好地理解。 Thank you very much in advance. 提前非常感谢您。

在此处输入图片说明

If df is your dataframe and columns are 0 and 1 : 如果df是您的数据帧,并且列是01

counts = df.groupby([0,1]).size().unstack()

# you could join in the bar labels here, for example:
# labels = pd.Series({1: "Internal Medicine", 2: "ENT Physicians", 3: "General Surgery"})
# counts = counts.join(labels.to_frame("label"))
# counts["label"] = counts["label"].fillna(counts.index)
# counts = counts.set_index("label")

# absolute numbers
counts.plot(kind="bar", stacked=True)

# percents
(counts / counts.sum()).plot(kind="bar", stacked=True)

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

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