我有以下代码:
grouped[['scene_average']].agg([np.mean, np.std, len])
我想用np.std
ddof=5
我怎么能这样做?
答案 0 :(得分:3)
使用lambda
功能:
grouped[['scene_average']].agg([np.mean, lambda x: np.std(x, ddof=5), len])
pandas函数的另一个解决方案mean
,std
:
grouped[['scene_average']].agg(['mean', lambda x: x.std(ddof=5), len])
样品:
np.random.seed(10)
df = pd.DataFrame({'scene_average':np.random.randint(10,size=20),
'a':np.random.randint(3,size=20)})
grouped = df.groupby('a')
df1 = grouped[['scene_average']].agg([np.mean, lambda x: np.std(x, ddof=5), len])
d = {'<lambda>':'std','len':'count'}
df1 = df1.rename(columns = d)
print (df1)
scene_average
mean std count
a
0 5.500000 NaN 4
1 3.555556 5.749396 9
2 5.000000 4.898979 7
如果在列中删除MultiIndex
:
df1.columns = df1.columns.map('_'.join)
print (df1)
scene_average_mean scene_average_std scene_average_count
a
0 5.500000 NaN 4
1 3.555556 5.749396 9
2 5.000000 4.898979 7