如何在熊猫中绘制箱形图?

时间:2020-02-16 06:50:42

标签: python-3.x pandas dataframe boxplot

我目前有这样的数据框:

jq --stream -c '(.[0]|index("tls.handshake.certificate_raw")) as $ix
            | select($ix) | .[0] |= .[$ix+1:]' Tshark.json |
jq -r -nc 'fromstream(inputs)[0]' | sort -u > Certificates.txt

此处是患者类型的索引

现在,我正在尝试创建一个总共有4个箱形图的熊猫箱形图,其中2个是| patient type | asir | aspr | |:----------------------------|-------:|--------:| | definitive dyalisis patient | 2975.6 | 15808.1 | | kidney transplant patient | 362 | 4469.3 | 的{​​{1}}和asir值以及另外2个为aspr

当前,我尝试使用以下代码对其进行编码:

definitive dialysis patient

但是最终看起来像这样: enter image description here

1 个答案:

答案 0 :(得分:1)

因此,我假设您需要每种类型的4个箱形图:asirasprdefinitive dyalisis patientkidney 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 |

绘制第一个显示asiraspr计数的图:

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

这表明, enter image description here

现在转置数据框以绘制definitive dyalisis patientkidney 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