为什么np.std()和ivot_table(aggfunc = np.std)返回不同的结果 [英] why np.std() and pivot_table(aggfunc=np.std) return the different result
问题描述
我有一些代码,不明白为什么会出现这种差异:
I have some code and do not understand why the difference occurs:
np.std()单独使用时默认ddof = 0.
np.std() which default ddof=0,when it's used alone.
但是为什么当它用作数据透视表(aggfunc = np.std)中的参数时,它会自动更改为ddof = 1.
but why when it's used as an argument in pivot_table(aggfunc=np.std),it changes into ddof=1 automatically.
import numpys as np
import pandas as pd
dft = pd.DataFrame({'A': ['one', 'one'],
'B': ['A', 'A'],
'C': ['bar', 'bar'],
'D': [-0.866740402,1.490732028]})
np.std(dft['D'])
#equivalent:np.std([-0.866740402,1.490732028]) (which:defaualt ddof=0)
#the result: 1.178736215
dft.pivot_table(index=['A', 'B'],columns='C',aggfunc=np.std)
#equivalent:np.std([-0.866740402,1.490732028],ddof=1)
#the result:1.666985
推荐答案
pivot使用DataFrame.groupby.agg,当您提供聚合函数时,它将尝试找出确切的方法来
pivot uses DataFrame.groupby.agg and when you supply an aggregation function it's going to try to figure out exactly how to _aggregate
.
arg=np.std
将在此处处理,相关代码为
arg=np.std
will get handled here, the relevant code being
f = self._get_cython_func(arg)
if f and not args and not kwargs:
return getattr(self, f)(), None
此表隐藏在DataFrame类中:
Hidden in the DataFrame class is this table:
pd.DataFrame()._cython_table
#OrderedDict([(<function sum>, 'sum'),
# (<function max>, 'max'),
# ...
# (<function numpy.std>, 'std'),
# (<function numpy.nancumsum>, 'cumsum')])
pd.DataFrame()._cython_table.get(np.std)
#'std'
因此,np.std
仅用于选择要调用的属性,默认值ddof
被完全忽略,而使用pandas
默认值ddof=1
.
And so np.std
is only used to select the attribute to call, the default ddof
are completely ignored, and instead the pandas
default of ddof=1
is used.
getattr(dft['D'], 'std')()
#1.6669847417133286
这篇关于为什么np.std()和ivot_table(aggfunc = np.std)返回不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!