使用Groupby函数绘制条形图,并绘制和简化
我正在尝试根据GroupBy函数绘制条形图,但是一旦尝试它崩溃并显示以下错误:
当用户从MultiSelect
widget中选择3个项目时,以下错误出现。
valueerror:所有参数均应具有相同的长度。长度 参数
颜色
是3,而先前正在处理的长度 参数['性别','count']是95
代码:
some_columns_df = df.loc[:,['gender','country','city','hoby','company','status']]
some_collumns = some_columns_df.columns.tolist()
select_box_var= st.selectbox("Choose X Column",some_collumns)
multiselect_var= st.multiselect("Select Columns To GroupBy",some_collumns)
test_g3 = df.groupby([select_box_var] + multiselect_var).size().reset_index(name='count')
fig = px.histogram(test_g3,x=select_box_var, y='count',color=multiselect_var ,barmode = 'group',text_auto = True)
我知道错误在color
px中的参数 px.histogram.histogram
i am trying to plot a bar chart based on groupby function but once i try it crash and display the below error:
this error below appear when the user select 3 items from the multiselect
widget.
ValueError: All arguments should have the same length. The length of
argumentcolor
is 3, whereas the length of previously-processed
arguments ['gender', 'count'] is 95
code:
some_columns_df = df.loc[:,['gender','country','city','hoby','company','status']]
some_collumns = some_columns_df.columns.tolist()
select_box_var= st.selectbox("Choose X Column",some_collumns)
multiselect_var= st.multiselect("Select Columns To GroupBy",some_collumns)
test_g3 = df.groupby([select_box_var] + multiselect_var).size().reset_index(name='count')
fig = px.histogram(test_g3,x=select_box_var, y='count',color=multiselect_var ,barmode = 'group',text_auto = True)
I know the error is in the color
parameter in the px.histogram
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是颜色只接受一个类别。
会导致
2
是List的长度
['Column_a'' ,'column_b']
,而244
是dataframe
的行。根据 document :
因此,要么我们使用
column_name
,要么使用series
。这是我的方法:
输出
列:
sex
唯一值<代码>女性和男性
time
带独特的值晚餐
和午餐
我选择这两列,更容易弄清楚只有
4组合。
我们创建一个
系列
contat列sex
和time
输出
使用此
类别
作为颜色参考如果
','。加入
没有工作,有问题,那么我们尝试另一种方式
sup [1]
The reason is color only accepts one category.
Would cause
2
is the length of list['column_a','column_b']
, while244
is thedataframe
's rows.According to the document:
Therefore, either we use a
column_name
, or we use aseries
.Here's my approach:
Output
Column:
sex
with unique valuesFemale
andMale
time
with unique valuesDinner
andLunch
I choose these two columns, it's easier to figure out that there is only
4 combination.
We create a
series
that concat columnssex
andtime
Output
Utilize this
categories
as color referenceIf
','.join
didn't work, having issue,then we try another way
Sup[1]