如何计算各组的平均值Pandas.dataframe像seaborn.factorp公司

2024-10-03 21:30:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个格式化为熊猫数据帧的数据集。请参见seabornhttp://seaborn.pydata.org/generated/seaborn.factorplot.html#seaborn.factorplot中的示例

>>> import seaborn as sns
>>> sns.set(style="ticks")
>>> exercise = sns.load_dataset("exercise")
>>> g = sns.factorplot(x="time", y="pulse", hue="kind", data=exercise)

与sns.factorplot公司,我可以按组查看数据的平均值(对于本例,图表按“种类”显示1/15/30分钟组的脉搏平均值)。在

我想直接得到图表中的“值”。 例如

^{pr2}$

我可以使用2深度循环来获得我想要的值,但我认为熊猫应该有更容易的东西,因为这是一个常见的要求。在

与matplotlib不同,matplotlib将返回绘图中的所有值,seaborn返回一个Facetgrid对象。Facetgrid似乎没有我想要的数据。在


Tags: 数据org示例matplotlibhtml图表seaborngenerated
1条回答
网友
1楼 · 发布于 2024-10-03 21:30:37

我想您需要^{}按列time和{}和^{}mean和{}:

print (exercise.groupby(['time','kind'])['pulse'].agg(['mean', 'std']))
#agg same as aggregate, only less typing ;)
#print (exercise.groupby(['time','kind'])['pulse'].aggregate(['mean', 'std']))
                 mean        std
time   kind                     
1 min  rest      90.2   6.545567
       walking   93.1   6.297266
       running   96.1   4.483302
15 min rest      90.9   6.118279
       walking   96.6   7.441625
       running  117.1  12.991023
30 min rest      91.4   5.337498
       walking   95.9   6.740425
       running  126.0  16.964014

^{pr2}$

相关问题 更多 >