3

I'm making my way around GroupBy, but I still need some help. Let's say that I've a DataFrame with columns Group, giving objects group number, some parameter R and spherical coordinates RA and Dec. Here is a mock DataFrame:

df = pd.DataFrame({ 
    'R'    : (-21.0,-21.5,-22.1,-23.7,-23.8,-20.4,-21.8,-19.3,-22.5,-24.7,-19.9),
    'RA': (154.362789,154.409301,154.419191,154.474165,154.424842,162.568516,8.355454,8.346812,8.728223,8.759622,8.799796),
    'Dec': (-0.495605,-0.453085,-0.481657,-0.614827,-0.584243,8.214719,8.355454,8.346812,8.728223,8.759622,8.799796),
    'Group': (1,1,1,1,1,2,2,2,2,2,2) 
})

I want to built a selection containing for each group the "brightest" object, i.e. the one with the smallest R (or the greatest absolute value, since Ris negative) and the 3 closest objects of the group (so I keep 4 objects in each group - we can assume that there is no group smaller than 4 objects if needed).

We assume here that we have defined the following functions:

#deg to rad
def d2r(x):
    return x * np.pi / 180.0

#rad to deg
def r2d(x):
    return x * 180.0 / np.pi

#Computes separation on a sphere
def calc_sep(phi1,theta1,phi2,theta2):
    return np.arccos(np.sin(theta1)*np.sin(theta2) + 
                     np.cos(theta1)*np.cos(theta2)*np.cos(phi2 - phi1) )

and that separation between two objects is given by r2d(calc_sep(RA1,Dec1,RA2,Dec2)), with RA1 as RA for the first object, and so on.

I can't figure out how to use GroupBy to achieve this...

4

2 回答 2

2

您可以在这里做的是构建一个更具体的帮助函数,该函数应用于每个“子框架”(每个组)。

GroupBy实际上只是一种工具,可以创建(组 id,DataFrame)对的迭代器之类的东西,并且当您调用.groupby().apply. (这掩盖了很多细节,如果您有兴趣,请参阅此处了解有关内部结构的一些详细信息。)

因此,在定义了三个基于 NumPy 的函数之后,还要定义:

def sep_df(df, keep=3):
    min_r = df.loc[df.R.argmin()]
    RA1, Dec1 = min_r.RA, min_r.Dec
    sep = r2d(calc_sep(RA1,Dec1,df['RA'], df['Dec']))
    idx = sep.nsmallest(keep+1).index
    return df.loc[idx]

然后只需应用,您就会得到一个 MultiIndex DataFrame,其中第一个索引级别是组。

print(df.groupby('Group').apply(sep_df))
              Dec  Group     R         RA
Group                                    
1     3  -0.61483      1 -23.7  154.47416
      2  -0.48166      1 -22.1  154.41919
      0  -0.49561      1 -21.0  154.36279
      4  -0.58424      1 -23.8  154.42484
2     8   8.72822      2 -22.5    8.72822
      10  8.79980      2 -19.9    8.79980
      6   8.35545      2 -21.8    8.35545
      9   8.75962      2 -24.7    8.75962

夹杂着一些评论:

def sep_df(df, keep=3):
    # Applied to each sub-Dataframe (this is what GroupBy does under the hood)

    # Get RA and Dec values at minimum R
    min_r = df.loc[df.R.argmin()]  # Series - row at which R is minimum
    RA1, Dec1 = min_r.RA, min_r.Dec  # Relevant 2 scalars within this row

    # Calculate separation for each pair including minimum R row
    # The result is a series of separations, same length as `df`
    sep = r2d(calc_sep(RA1,Dec1,df['RA'], df['Dec']))

    # Get index values of `keep` (default 3) smallest results
    # Retain `keep+1` values because one will be the minimum R
    # row where separation=0
    idx = sep.nsmallest(keep+1).index

    # Restrict the result to those 3 index labels + your minimum R
    return df.loc[idx]

为了速度,如果结果仍然适合您,请考虑传递sort=False给 GroupBy。

于 2017-09-21T15:54:17.860 回答
2

我想为每个组构建一​​个包含“最亮”对象的选择...以及该组中最接近的 3 个对象

步骤1:

为每组中最亮的对象创建一个数据框

maxR = df.sort_values('R').groupby('Group')['Group', 'Dec', 'RA'].head(1)

第2步:

合并两帧Group并计算分离

merged = df.merge(maxR, on = 'Group', suffixes=['', '_max'])
merged['sep'] = merged.apply(
    lambda x: r2d(calc_sep(x.RA, x.Dec, x.RA_max, x.Dec_max)), 
    axis=1
)

第 3 步:

排序数据帧,分组'Group'(可选)丢弃中间字段并从每组中取出前 4 行

finaldf = merged.sort_values(['Group', 'sep'], ascending=[1,1]
).groupby('Group')[df.columns].head(4)

使用您的示例数据生成以下数据框:

         Dec  Group     R          RA
4  -0.584243      1 -23.8  154.424842
3  -0.614827      1 -23.7  154.474165
2  -0.481657      1 -22.1  154.419191
0  -0.495605      1 -21.0  154.362789
9   8.759622      2 -24.7    8.759622
8   8.728223      2 -22.5    8.728223
10  8.799796      2 -19.9    8.799796
6   8.355454      2 -21.8    8.355454
于 2017-09-21T16:45:12.667 回答