简体   繁体   English

在 Python 中,有没有办法根据两列 groupby 表中的第一列创建条形图?

[英]In Python is there a way to create a bar chart based on the first column in a two-column groupby table?

I want to create a bar chart based on the first column in a two-column groupby table.我想根据两列 groupby 表中的第一列创建条形图。 I've summed up my dataframe by applying groupby to create the table I want.我通过应用 groupby 来创建我想要的表来总结我的 dataframe。 My groupby table currently only has two columns, the first being the categories, and the second being the count of those categories from my dataframe.我的 groupby 表目前只有两列,第一列是类别,第二列是我的 dataframe 中这些类别的计数。 I'd like to create a bar chart with plotly express using the first column as the x-axis, and then second column as the y-axis.我想创建一个带有 plotly express 的条形图,使用第一列作为 x 轴,然后将第二列作为 y 轴。 However, when I applied groupby to the dataframe it grouped my data how I want but that the first column doesn't have a label.但是,当我将 groupby 应用于 dataframe 时,它将我的数据按我想要的方式分组,但第一列没有 label。 In order to create a bar chart I need to tell plotly what the x-axis will be but there's no label in the groupby table.为了创建条形图,我需要告诉 plotly x 轴是什么,但 groupby 表中没有 label。

This is what I currently have for my groupby table:这是我目前的 groupby 表:

# Selection list

department = df['Provider Type'].unique().tolist()

departments_selection = st.multiselect('Program:',
                                        department,
                                        default=department)

# Filter dataframe based on selection

mask = df['Provider Type'].isin(departments_selection)
number_of_result = df[mask].shape[0]
st.markdown(f'*Available Results: {number_of_result}*')

## Booked
df_grouped = df[mask].groupby(['Provider Type'])['Provider Type'].count()
st.dataframe(df_grouped)

This is the output, which is what I want:这是 output,这就是我想要的: 在此处输入图像描述

As you can see the first column has no label so I can't reference it when trying to create a bar chart.如您所见,第一列没有 label 所以我在尝试创建条形图时无法引用它。 So I end up getting a bar chart with just numbers as labels when I'd really just like to use the first column's categories as labels.因此,当我真的只想使用第一列的类别作为标签时,我最终得到了一个只有数字作为标签的条形图。 This is what I have:这就是我所拥有的:

# Bar chart
pie_chart = px.bar(df_grouped,
                x="Provider Type",
                y='Provider Type',
                template='plotly_white')

st.plotly_chart(pie_chart)

This is the output:这是 output: 在此处输入图像描述

Can someone help me fix my x-axis so it shows the categories like in my groupby table?有人可以帮我修复我的 x 轴,以便它显示我的 groupby 表中的类别吗? I'm just not sure how to tell it to look for that first column since it has no label.我只是不确定如何告诉它查找第一列,因为它没有 label。

You can pass to px.bar for the x argument df_grouped.index .您可以将x参数df_grouped.index px.bar

Because no data was presented in the question i can't do the group by but this code example should show you how to fix the x-axis labels:因为问题中没有提供数据,所以我无法进行分组,但此代码示例应该向您展示如何修复 x 轴标签:

import pandas as pd
import plotly.express as px


d = {'Provider Type': [340, 861, 446, 148, 1484, 953, 347, 615]}
index = ['Community Health navigation', 'Dietitian', 'Kinesiology', 
         'MH Navigation', 'MH Therapist', 'Nursing', 'Pharmacist', 'Specialist']
df = pd.DataFrame(data=d, index=index)

fig = px.bar(df, y='Provider Type', x=df.index)
fig.show()

Output: Output:

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM