分组变量以绘制多级条形图
您能帮我帮助我吗?我有一个具有可变数量的产品(产品)的数据集,该数据集将离散值从1到3(随附)获取。然后,我有一个男性变量(性别)1,女性为0。我想绘制一个多级条形图,在x轴上我有数量的产品(prod),而在y轴上,我具有由性别分组的这些产品的总价值。我需要创建一个“计数”变量,该变量计算每个“性别”类别中每个“产品”的观察值。要分组和绘制变量,我使用以下代码(不起作用):
#Group the variables
grouped_gender['count'] = main_data.groupby(['Prod', 'Gender'])[['Prod']].count()
grouped_gender = pd.DataFrame(grouped_gender)
#Plot
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(10, 7))
barplot2 = sns.barplot(
data=grouped_gender,
x='Prod',
y='count',
hue='Gender',
orient='v',
ax = axes,
ci=None,
dodge=False
)
您可以帮助我确定问题吗?
Can you please help me with the following. I have a dataset with a variable - number of products (Prod) that takes discrete values from 1 to 3 (included). Then I have a variable (Gender) 1 for males, 0 for females. I want to plot a multilevel bar chart where on the x-axis I have number of products (Prod) and on the y-axis I have total value of these products that are grouped by the Gender. I need to create a 'count' variable that counts how many observations of each 'Prod' are in each 'Gender' category. To group and plot the variables I use the following code (which does not work):
#Group the variables
grouped_gender['count'] = main_data.groupby(['Prod', 'Gender'])[['Prod']].count()
grouped_gender = pd.DataFrame(grouped_gender)
#Plot
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(10, 7))
barplot2 = sns.barplot(
data=grouped_gender,
x='Prod',
y='count',
hue='Gender',
orient='v',
ax = axes,
ci=None,
dodge=False
)
Can you please help me to identify the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您可以将数据框放在与我的类似状态下,
这会产生以下结果
Assuming you can put your DataFrame in a similar state as mine
This produces the following result
使用
Countplot
在原始数据集上:输出:
注意:如果您需要数据,而不仅仅是图,请使用:
它给您:
Use
countplot
on the original dataset:Output:
Note: if you want the data, not just the plot, use:
which gives you: