gpt4 book ai didi

python - 如何为 Pandas 数据框构建并排箱线图

转载 作者:太空宇宙 更新时间:2023-11-04 04:04:55 26 4
gpt4 key购买 nike

男人和女人在标有“性别”的栏中。我想根据他们的幸福程度来绘制它们。所以,一张图两列一行。

我试图提取每个性别:

men = df[df['sex'] == 'Men']
women = df[df['sex'] == 'Women']
df_happy_sex = df[['happy', 'sex']].copy()

![ https://ibb.co/yqmWKkf]

最佳答案

import pandas as pd
import matplotlib.pyplot as plt

我已经为您的数据框创建了一个假想样本。

data = [['men', 55], ['men', 77], ['women', 85],
['men', 70], ['women', 68], ['women', 64],
['men', 86], ['men', 64], ['women', 54],
['men', 43], ['women', 86], ['women', 91]]

df = pd.DataFrame(data, columns = ['sex', 'happy'])

你可以:

df.boxplot(by=['sex'], sym ='', figsize = [6, 6])

它产生:我想这就是您想要的。

Hapiness

关于python - 如何为 Pandas 数据框构建并排箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com