您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:21:33 python 字数 4856 阅读 509 来源 www.jb51.cc/python

我想在python中创建一个条形图,其中包含多个x类别,数据计数为“是”或“否”.我已经开始使用一些代码,但我相信我正在以一种缓慢的方式获得我想要的解决方案.我可以使用seaborn,Matplotlib或pandas但不是Bokeh的解决方案,因为我想制作可扩展的出版品质数字.最终我想要的是:>条形图,在x轴上分类为“独木舟”,“巡航”,“皮划艇”和

概述

我想在python中创建一个条形图,其中包含多个x类别,数据计数为“是”或“否”.我已经开始使用一些代码,但我相信我正在以一种缓慢的方式获得我想要的解决方案.我可以使用seaborn,Matplotlib或pandas但不是bokeh解决方案,因为我想制作可扩展的出版品质数字.

最终我想要的是:

>条形图,在x轴上分类为“独木舟”,“巡航”,“皮划艇”和“船”
>按“颜色”分组,所以绿色或红色
>显示“是”响应的比例:所以是行的数量除以“红色”和“绿色”的数量,在这种情况下是4红色和4绿色,但这可能会改变.

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

import pandas as pd
data = [{'ship': 'Yes','canoe': 'Yes','cruise': 'Yes','kayak': 'No','color': 'Red'},{'ship': 'Yes','kayak': 'Yes','canoe': 'No','color': 'Green'},{'ship': 'No','cruise': '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类别,仅显示“是”响应的结果,并将其视为“是”而不仅仅是计数的比例.救命?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from itertools import groupby

data = [{'ship': 'Yes','color': 'Red'}]
df = pd.DataFrame(data)
df1 = df.replace(["Yes","No"],[1,0]).groupby("color").mean().stack().rename('% Yes').to_frame()


def add_line(ax,xpos,ypos):
    line = plt.Line2D([xpos,xpos],[ypos + .1,ypos],transform=ax.transAxes,color='gray')
    line.set_clip_on(False)
    ax.add_line(line)

def label_len(my_index,level):
    labels = my_index.get_level_values(level)
    return [(k,sum(1 for i in g)) for k,g in groupby(labels)]

def label_group_bar_table(ax,df):
    ypos = -.1
    scale = 1./df.index.size
    for level in range(df.index.nlevels)[::-1]:
        pos = 0
        for label,rpos in label_len(df.index,level):
            lxpos = (pos + .5 * rpos)*scale
            ax.text(lxpos,ypos,label,ha='center',transform=ax.transAxes)
            add_line(ax,pos*scale,ypos)
            pos += rpos
        add_line(ax,ypos)
        ypos -= .1


colorlist = ['green','red']
cp = sns.color_palette(colorlist)

ax = sns.barplot(x=df1.index,y='% Yes',hue = df1.index.get_level_values(0),data=df1,palette=cp)
#Below 2 lines remove default labels
ax.set_xticklabels('')
ax.set_xlabel('')
label_group_bar_table(ax,df1)

输出

enter image description here

总结

以上是编程之家为你收集整理的python – 使用groupby和pandas dataframe中的多个列从字符串数据创建条形图全部内容,希望文章能够帮你解决python – 使用groupby和pandas dataframe中的多个列从字符串数据创建条形图所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶