python - Seaborn和 Pandas : Make multiple x-category bar plot using multi index data in python

标签 python pandas dataframe seaborn

我有一个多索引数据框,我已将其融化为如下所示:

Color   Frequency           variable        value
Red     2-3 times a month   x               22
Red     A few days a week   x               45
Red     At least once a day x               344
Red     Never               x               5
Red     Once a month        x               1
Red     Once a week         x               0
Red     Once every few months x             4
Blue    2-3 times a month   x               4
Blue    A few days a week   x               49
Blue    At least once a day x               200
Blue    Never               x               7
Blue    Once a month        x               19
Blue    Once a week         x               10
Blue    Once every few months x             5
Red     2-3 times a month   y               3
Red     A few days a week   y               97
Red     At least once a day y               144
Red     Never               y               4
Red     Once a month        y               0
Red     Once a week         y               0
Red     Once every few months y             4
Blue    2-3 times a month   y               44
Blue    A few days a week   y               62
Blue    At least once a day y               300
Blue    Never               y               2
Blue    Once a month        y               4
Blue    Once a week         y               23
Blue    Once every few months y             6
Red     2-3 times a month   z               4
Red     A few days a week   z               12
Red     At least once a day z               101
Red     Never               z               0
Red     Once a month        z               0
Red     Once a week         z               10
Red     Once every few months z             0
Blue    2-3 times a month   z               100
Blue    A few days a week   z               203
Blue    At least once a day z               299
Blue    Never               z               0
Blue    Once a month        z               0
Blue    Once a week         z               204
Blue    Once every few months z             100

我正在尝试制作一个seaborn图,其中x轴变量频率有两个类别,色调基于Color。此外,我希望 y 轴是每个 Colorvalue 与该 variable 值之和的比例;例如变量“每月 x.2-3 次”的 y 值应为 22/(22+45+344+5+1+0+4) 或 5.22%。

到目前为止我有这个:

import seaborn as sns
fig, ax1 = plt.subplots(figsize=(20, 10))
sns.factorplot(x='variable',y='value', hue='Frequency', data=df, kind='bar', ax=ax1)

这是到达那里的路的一部分。我如何还按 1) 颜色和 2) 获取每个变量频率值的比例,而不是计数?

最佳答案

这是您需要查找该组每个数字的部分的内容:

df['proportion'] = df['value'] / df.groupby(['Color','variable'])['value'].transform('sum')

输出:

   variable              Frequency Color  value   portion
0         x      2-3 times a month   Red     22  0.052257
1         x      A few days a week   Red     45  0.106888
2         x    At least once a day   Red    344  0.817102
3         x                  Never   Red      5  0.011876
4         x           Once a month   Red      1  0.002375
5         x            Once a week   Red      0  0.000000
6         x  Once every few months   Red      4  0.009501
7         x      2-3 times a month  Blue      4  0.013605
8         x      A few days a week  Blue     49  0.166667
9         x    At least once a day  Blue    200  0.680272
10        x                  Never  Blue      7  0.023810
11        x           Once a month  Blue     19  0.064626
12        x            Once a week  Blue     10  0.034014
13        x  Once every few months  Blue      5  0.017007
14        y      2-3 times a month   Red      3  0.011905
15        y      A few days a week   Red     97  0.384921
16        y    At least once a day   Red    144  0.571429
17        y                  Never   Red      4  0.015873
18        y           Once a month   Red      0  0.000000
19        y            Once a week   Red      0  0.000000
20        y  Once every few months   Red      4  0.015873
21        y      2-3 times a month  Blue     44  0.099773
22        y      A few days a week  Blue     62  0.140590
23        y    At least once a day  Blue    300  0.680272
24        y                  Never  Blue      2  0.004535
25        y           Once a month  Blue      4  0.009070
26        y            Once a week  Blue     23  0.052154
27        y  Once every few months  Blue      6  0.013605
28        z      2-3 times a month   Red      4  0.031496
29        z      A few days a week   Red     12  0.094488
30        z    At least once a day   Red    101  0.795276
31        z                  Never   Red      0  0.000000
32        z           Once a month   Red      0  0.000000
33        z            Once a week   Red     10  0.078740
34        z  Once every few months   Red      0  0.000000
35        z      2-3 times a month  Blue    100  0.110375
36        z      A few days a week  Blue    203  0.224062
37        z    At least once a day  Blue    299  0.330022
38        z                  Never  Blue      0  0.000000
39        z           Once a month  Blue      0  0.000000
40        z            Once a week  Blue    204  0.225166
41        z  Once every few months  Blue    100  0.110375

关于python - Seaborn和 Pandas : Make multiple x-category bar plot using multi index data in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51546293/

相关文章:

python - 接收SettingWithCopyWarning。继续安全吗?

python - Django URL 排序

python - 如何在 python 中设置本地主机?

python - Python 范围函数如何在实际参数之前有一个默认参数?

python - 更新数据框 ID w.r.t 缺少日期列值

r - 尝试对数据框中的字段运行方差分析 [R]

python - 我的 Python 版本的玩具 SVM 出了什么问题?

python - 一次随机将 Pandas 数据框分成几组以进行 x 折交叉验证

python - 如何重新排列 Pandas 列序列?

python - 如果键属性不存在,则解析 json 文件时获取 0 条记录