代码之家  ›  专栏  ›  技术社区  ›  Svenno Nito

Python:使用groupby-wird输出绘制熊猫数据帧

  •  3
  • Svenno Nito  · 技术社区  · 7 年前

    我有一个dataframe(类型为object,用于在特定步骤中插入列表),有三列,如下所示

        data  stimulus trial
    0   2     -2       1
    1   2     -2       2
    2   2     -2       3
    3   2     -2       4
    4   2     -2       5
    5   2     -2       6
    6   1     -2       7
    ...
    159 1     2.5      16
    

    # spi_num is my dataframe
    sns.swarmplot(x="stimulus", y="data", data=spi_num.astype(np.float), edgecolor="black", linewidth=.9)
    sns.boxplot(x="stimulus", y="data", data=spi_num.astype(np.float), saturation=1)
    

    我有两个问题。如何将传奇与seaborn顺利融合?我如何使用pandas plot命令得到这个绘图?我想我需要这样的东西:

    spi_num.astype(np.float).groupby('stimulus').plot.box()
    

    然后我得到10个数字(每个刺激一个),每个xlabel有3个箱线图,即“数据”、“刺激”和“试验”。这难道不应该给我一个如上所示的情节吗?至少 he does it like this .

    enter image description here


    构建我的数据框架

    trial_vec    = np.tile(np.arange(16)+1, 10)     
    stimulus_vec = np.repeat([-2., -1.75, -1., -0.75, -0.5,  0.5,  1.,  1.25,  1.75,  2.5 ], 16)                  
    data_vec     = np.random.randint(0, 16, size=160)
    spi_num      = pd.DataFrame({'trial': trial_vec, 'stimulus': stimulus_vec, 'data': data_vec}).astype('object')
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   ImportanceOfBeingErnest    7 年前

    您可以使用 DataFrame.boxplot 获取所需的方框图

    spi_num.astype(np.float).boxplot(column="data", by="stimulus")
    

    enter image description here