gpt4 book ai didi

python - 使用 groupby 和 pandas 数据框中的多列从字符串数据创建条形图

转载 作者:太空狗 更新时间:2023-10-30 01:18:51 25 4
gpt4 key购买 nike

我想在 python 中根据"is"或“否”的数据计数制作一个具有多个 x 类别的条形图。我已经开始编写一些代码,但我相信我正在以缓慢的方式获得我想要的解决方案。我会接受使用 seaborn、Matplotlib 或 pandas 但 Bokeh 的解决方案,因为我想制作可缩放的出版物质量数据。

最终我想要的是:

  • 在 x 轴上带有类别“canoe”、“cruise”、“kayak”和“ship”的条形图
  • 按“颜色”分组,因此要么是绿色要么是红色
  • 显示"is"响应的比例:是的行数除以“红色”和“绿色”的数量,在本例中为 4 红色和 4 绿色,但这可能会改变。

这是我正在使用的数据集:

import pandas as pd
data = [{'ship': 'Yes','canoe': 'Yes', 'cruise': 'Yes', 'kayak': 'No','color': 'Red'},{'ship': 'Yes', 'cruise': 'Yes', 'kayak': 'Yes','canoe': 'No','color': 'Green'},{'ship': 'Yes', 'cruise': 'Yes', 'kayak': 'No','canoe': 'No','color': 'Green'},{'ship': 'Yes', 'cruise': 'Yes', 'kayak': 'No','canoe': 'No','color': 'Red'},{'ship': 'Yes', 'cruise': 'Yes', 'kayak': 'Yes','canoe': 'No','color': 'Red'},{'ship': 'No', 'cruise': 'Yes', 'kayak': 'No','canoe': 'Yes','color': 'Green'},{'ship': 'No', 'cruise': 'No', 'kayak': 'No','canoe': 'No','color': 'Green'},{'ship': 'No', 'cruise': 'No', 'kayak': 'No','canoe': 'No','color': 'Red'}]
df = pd.DataFrame(data)

这是我开始的:

print(df['color'].value_counts())

red = 4 # there must be a better way to code this rather than manually. Perhaps using len()?
green = 4

# get count per type
ca = df['canoe'].value_counts()
cr = df['cruise'].value_counts()
ka = df['kayak'].value_counts()
sh = df['ship'].value_counts()
print(ca, cr, ka, sh)

# group by color
cac = df.groupby(['canoe','color'])
crc = df.groupby(['cruise','color'])
kac = df.groupby(['kayak','color'])
shc = df.groupby(['ship','color'])

# make plots
cac2 = cac['color'].value_counts().unstack()
cac2.plot(kind='bar', title = 'Canoe by color')

enter image description here

但我真正想要的是所有 x 类别都在一个图上,只显示"is"响应的结果,并作为"is"的比例而不是仅仅计数。帮忙?

最佳答案

不确定我是否正确理解了问题。查看每种船型颜色的答案比例似乎更有意义。

import matplotlib.pyplot as plt
import pandas as pd
data = [{'ship': 'Yes','canoe': 'Yes', 'cruise': 'Yes', 'kayak': 'No','color': 'Red'},{'ship': 'Yes', 'cruise': 'Yes', 'kayak': 'Yes','canoe': 'No','color': 'Green'},{'ship': 'Yes', 'cruise': 'Yes', 'kayak': 'No','canoe': 'No','color': 'Green'},{'ship': 'Yes', 'cruise': 'Yes', 'kayak': 'No','canoe': 'No','color': 'Red'},{'ship': 'Yes', 'cruise': 'Yes', 'kayak': 'Yes','canoe': 'No','color': 'Red'},{'ship': 'No', 'cruise': 'Yes', 'kayak': 'No','canoe': 'Yes','color': 'Green'},{'ship': 'No', 'cruise': 'No', 'kayak': 'No','canoe': 'No','color': 'Green'},{'ship': 'No', 'cruise': 'No', 'kayak': 'No','canoe': 'No','color': 'Red'}]
df = pd.DataFrame(data)

ax = df.replace(["Yes","No"],[1,0]).groupby("color").mean().transpose().plot.bar(color=["g","r"])
ax.set_title('Proportion "Yes" answers per of boat type and color')
plt.show()

enter image description here

这意味着例如25% 的绿色独木舟回答"is"。

关于python - 使用 groupby 和 pandas 数据框中的多列从字符串数据创建条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51532581/

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