groupby 申请未给出预期结果

groupby apply not giving expected result

我是 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 函数定义为:

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 

这里有什么问题?

将角度从度数转换为弧度后使用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

您可以指定要在 apply() 上使用的 SeriesGroupBy 对象。

df.groupby(['Profile','bin'])["inflow_direction"].apply(circstd) 会做的工作。

输出:

Profile  bin
1        51     0.029650
         66     0.002367
Name: inflow_direction, dtype: float64