I have a dataset with multiple rows and columns. Simplifying:
A B
g 1
h 2
h 3
g 3
j 4
g 5
Where A is a code for a surgery and B is the length of stay in a hospital. So, each of the rows corresponds to a patient.
I would like to do multiple plots (one for each category) of columns A and B. In this example:
Plot 1: One plot where the x represents every patient, that went under a "g" surgery and the y represents the lenght of stay of each patient. So for this plot, must exist 3 bars, since there are 3 patients with "g" surgery;
Plot 2: One plot where the x represents every patient, that went under a "h" surgery and the y represents the lenght of stay of each patient. So for this plot, must exist 2 bars, since there are 2 patients with "h" surgery;
Plot 3: One plot where the x represents every patient, that went under a "j" surgery and the y represents the lenght of stay of each patient. So for this plot, must exist 1 bars, since there are 1 patients with "j" surgery;
Can anyone help me?
You can do this, get dataset in pandas as df:
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> df = pd.DataFrame({'A':['g', 'h', 'h', 'g', 'j', 'g'], 'B':[1, 2, 3, 3, 4, 5]})
>>> df
A B
0 g 1
1 h 2
2 h 3
3 g 3
4 j 4
5 g 5
>>> df.groupby('A').plot(kind = 'bar')
A
g AxesSubplot(0.125,0.11;0.775x0.77)
h AxesSubplot(0.125,0.11;0.775x0.77)
j AxesSubplot(0.125,0.11;0.775x0.77)
dtype: object
>>> plt.show() #you'll see 3 different figures for that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.