在使用 `groupby` 之后使用 Seaborn 的 `factorplot`

Using Seaborn's `factorplot`after using `groupby`

我从一个非常大的 Pandas 数据框开始,格式为:

DAY     FLAVOR    PRICE    #CREATED    BOUGHT
 1       apple      5         1         1
 1       apple      5         3         7
 1       apple      5         4         2
 1       apple      5         5         3
 2       apple      5         1         1
 2       apple      5         3         9
 2       apple      5         4         8
                 . 
                 .  
                 . 

完成 groupby(['DAY','FLAVOR','PRICE']).mean() 后,我得到了一个类似于以下内容的简化数据帧:

DAY     FLAVOR    PRICE    BOUGHT
 1       apple      5       3.4
                   25       2.9
         cherry     5       1.7
                   25       2.6
 2       apple      5       1.6
                   25       1.7
         cherry     5       3.1
                   25       3.2
                 . 
                 . 
                 . 

此时,我想创建以下 Seaborn 因子图:X = DAY,Y = BOUGHT,Hue = FLAVOR。在绘图之前我需要融化这个 "reduced" 数据框吗?或者有没有一种简单的方法来绘制我当前数据框中的数据?

您可以执行 df.reset_index(),然后选择适当的列来绘制因子图。

这意味着:

fg = (
    df.groupby(['DAY', 'FLAVOR', 'PRICE'])
      .mean()
      .reset_index()
      .pipe((sns.factorplot, 'data'), x='DAY', y='BOUGHT', hue='FLAVOR')
)