简体   繁体   English

如何使用 Pandas 中的数据透视表计算标准偏差?

[英]How do I calculate the standard deviation with a pivot table in Pandas?

I have a bunch of data involving certain numbers for certain players of specific sports.我有一堆数据,涉及特定运动的某些运动员的某些数字。 I want to use pivot tables in Pandas to have it split up the data by sport, and for the corresponding value for each sport have the mean "number" value for all people who play that sport.我想在 Pandas 中使用数据透视表来按运动拆分数据,并且对于每项运动的相应值,具有所有参加该运动的人的平均“数字”值。 (So if it were basketball, it would average the number of all the players who play basketball, and the number basically represents a preference.) (所以如果是篮球,它会平均所有打篮球的球员的人数,这个数字基本上代表了一种偏好。)

I can do this pretty easily with pivot tables, but if I wanted to do the same thing for calculating the standard deviation, I cannot figure out how.我可以用数据透视表很容易地做到这一点,但如果我想为计算标准偏差做同样的事情,我不知道如何做。 I can do np.mean for the mean, but there's no np.std .我可以做np.mean的平均值,但没有np.std I know there's std() but I'm unsure how I'd use it in this context.我知道有std()但我不确定在这种情况下如何使用它。

Are pivot tables not advisable for doing this task?是否不建议执行此任务的数据透视表? How should I find the standard deviation for the numeric data of all players of a specific sport?我应该如何找到特定运动的所有运动员的数字数据的标准偏差?

如果您有一个 DataFrame ( df ),其中有一列名为"sport" ,它很简单:

df.groupby(by=['sport']).std()

What version of numpy are you using?你用的是什么版本的numpy? 1.9.2 has np.std: 1.9.2 有 np.std:

np.std?
Type:        function
String form: <function std at 0x0000000003EE47B8>
File:        c:\anaconda3\lib\site-packages\numpy\core\fromnumeric.py
Definition:  np.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False)
Docstring:
Compute the standard deviation along the specified axis.

Returns the standard deviation, a measure of the spread of a distribution,
of the array elements. The standard deviation is computed for the
flattened array by default, otherwise over the specified axis.
df.pivot_table(values='number', index='sport', aggfunc='std')

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

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