我是python的新手,我正在尝试找到循环数据的标准(风向)按一些特征对数据进行分组。 这是我正在使用的一组df。
Profile bin inflow_direction
0 1 51 331.7
1 1 51 332.8
2 1 51 334.1
3 1 51 335.4
4 1 51 336.4
5 1 66 337.3
6 1 66 337.5
7 1 66 337.6
8 1 66 337.7
9 1 66 337.6
我需要每个配置文件中每个bin组的std。 我已将std函数定义为:
def circstd(j) :
samples = np.radians (j)
return scipy.stats.circstd(samples, high=6.283185307179586, low=0, axis=None)
当我分组时:
df.groupby(['Profile','bin']).apply(circstd)
出局是:
idscng_f bin
1 51 0.567811
66 0.671470
但我期待
idscng_f bin
1 51 0.0296
66 0.0025
这里有什么问题?
答案 0 :(得分:1)
将角度从度数转换为弧度后使用np.std
:
def simple_circstd(j) :
return np.std(np.radians(j))['inflow_direction']
执行Groupby
:
df.groupby(['Profile','bin']).apply(simple_circtd)
获得的结果输出:
Profile bin
1 51 0.029650
66 0.002367
dtype: float64
答案 1 :(得分:1)
您可以指定SeriesGroupBy
对象使用apply()
。
df.groupby(['Profile','bin'])["inflow_direction"].apply(circstd)
我会做的。
输出:
Profile bin
1 51 0.029650
66 0.002367
Name: inflow_direction, dtype: float64