Как нарисовать этот квадратный график в pandas? - PullRequest
1 голос
/ 16 февраля 2020

У меня в настоящее время есть этот фрейм данных, подобный этому:

| patient type                |   asir |    aspr |
|:----------------------------|-------:|--------:|
| definitive dyalisis patient | 2975.6 | 15808.1 |
| kidney transplant patient   |  362   |  4469.3 |

Здесь тип пациента - это индекс

Сейчас я пытаюсь создать pandas блочный график, который имеет в общей сложности 4 коробочных графика, 2 из которых являются значениями asir и aspr для definitive dialysis patient и еще 2 для kidney transplant patient.

В настоящее время я пытался кодировать это с помощью следующего кода:

%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# import the csv file
dataname = 'Datasets\\age-standardised-incidence-rate-and-prevalence-rate-for-definitive-dialysis-and-transplant.csv'
data = pd.read_csv(dataname)
df = pd.DataFrame(data)

# drop the year column away as we want together all the definitive dialysis patient data and the kidney transplant patient data together.abs
df2 = df.drop(['year'],axis=1)

# sum up all the data for the respective patien_type according to their respective asir and aspr
df3 = df2.groupby(df2['patient_type']).sum()
df4 = df3.rename(columns = {'patient_type':'patient_type','asir':'asir','aspr':'aspr'})

# pivot the table so that we can use the patient_type to plot out the bar plot
# df4 = df4.pivot_table(columns=['patient_type'])

# plot out the box plot 
bplot = df4.boxplot(by='patient_type',column=['asir','aspr'],grid=True,figsize=(40,20),patch_artist=True,fontsize=20)
plt.title('Boxplot Of Age Standardised incidence rate and prevalence rate for definitive dialysis and transplant patients.',fontsize=20)
# plt.legend(df4['patient_type'],fontsize=20)
plt.show()
df4 

Но в итоге все выглядело так: enter image description here

1 Ответ

1 голос
/ 16 февраля 2020

Итак, я предполагаю, что вы хотите 4 коробочных участка с каждым типом: asir, aspr, definitive dyalisis patient и kidney transplant patient.

Допустим, ваш начальный кадр данных выглядит следующим образом:

df = pd.DataFrame({'asir': [2975.6, 362.0], 'aspr':[15808.1, 4469.3],
    'patient type': ['definitive dyalisis patient',
            'kidney transplant patient']})
| patient type                |   asir |    aspr |
|:----------------------------|-------:|--------:|
| definitive dyalisis patient | 2975.6 | 15808.1 |
| kidney transplant patient   |  362   |  4469.3 |

Нарисуйте первый график, на котором мы показываем asir и количество aspr:

df = df.set_index('patient type')
df.boxplot(grid=True,figsize=(40,20),patch_artist=True,fontsize=20)

Это показывает, enter image description here

Теперь транспонируйте ваш фрейм данных для построения definitive dyalisis patient и kidney transplant process.

t_df = df.T

Фрейм данных выглядит вот так:

|      |   definitive dyalisis patient |   kidney transplant patient |
|:-----|------------------------------:|----------------------------:|
| asir |                        2975.6 |                       362   |
| aspr |                       15808.1 |                      4469.3 |

Теперь нарисуйте это так же, как раньше:

t_df.boxplot(grid=True,figsize=(40,20),patch_artist=True,fontsize=20)

Сюжет выглядит так: enter image description here

...