简体   繁体   English

pandas - 分组加权条形图

[英]pandas - Grouped weighted Bar Chart

Consider the following DataFrame consisting of 10 rows.考虑以下由 10 行组成的 DataFrame。

d = {
    'grp_id':[1,2,1,1,1,3,1,1,4,1],
    'weight':[1,2,1,1,1,3,1,1,4,4],
    'value': [1,2,1,3,2,1,4,1,1,3]
}
df = pd.DataFrame(d)

A weighted histogram can be achieved with加权直方图可以通过

df['value'].hist(histtype='bar', weights=df['weight'])

An unweighted bar chart grouped by grp_id withgrp_id分组的未加权条形图

df['value'].hist(by=df['grp_id'], histtype='bar')

在此处输入图片说明

I'd like to combine the two and plot a weighted bar chart grouped by grp_id .我想将两者结合起来并绘制一个按grp_id分组的加权条形图。
I've tried the following 2 methods without success since for both I get a ValueError .我尝试了以下 2 种方法但没有成功,因为我都得到了ValueError

df['value'].hist(by=df['grp_id'], weights=df['weight'], histtype='bar')
df['value'].hist(by=df['grp_id'], weights='weight', histtype='bar')

ValueError: weights should have the same shape as x ValueError:权重应该与 x 具有相同的形状

The temporary solution I'm using is the following.我正在使用的临时解决方案如下。

fig, axes = plt.subplots(2, 2)
for ax,(idx, grp) in zip(axes.flatten(), df.groupby('grp_id')):
    grp['value'].hist(weights=grp['weight'], histtype='bar', ax=ax)

However, I would like to ask if there is a direct way to do it with pandas.但是,我想问一下是否有直接的方法可以用熊猫来做到这一点。

I would firstly create a new data frame storing weighted values:我将首先创建一个存储加权值的新数据框:

df['weighted_values'] = df.weight*df.value
df = df.groupby('grp_id')['weighted_values'].sum().to_frame().reset_index()

You can use seaborn to plot the final barchart aesthetically:您可以使用 seaborn 从美学上绘制最终的条形图:

import seaborn as sns
sns.barplot(x = 'grp_id', y = 'weighted_values', data=df)

在此处输入图片说明

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

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