在每组熊猫数据框中创建一个点列表

时间:2020-01-04 13:07:28

标签: python pandas

我有一个数据集,结构如下example_df

example_df = pd.DataFrame({'measurement_id': np.concatenate([[0] * 300, [1] * 300]),
                           'min': np.concatenate([np.repeat(range(0, 30), 10), 
                                                  np.repeat(range(0, 30), 10)]),
                           'grp': list(np.repeat(['A', 'B'], 5)) * 60,
                           'grp2': list(np.random.choice([0, 1, 2], 10)) * 60,
                           'obj': np.array(list(range(0, 10)) * 60),
                           'x': np.random.normal(0.0, 10.0, 600),
                           'y': np.random.normal(50.0, 40.0, 600)})

我还有一个函数将一组点列表作为输入并执行一些计算。我想准备数据并在分组数据框中创建一个点列表。

我当前的解决方案如下:

def df_to_points(df):
    points = []
    for index, row in df.iterrows():
        points.append(tuple(row))
    return(points)

res = example_df \
    .groupby(['measurement_id', 'min', 'grp']) \
    .apply(lambda x: [df_to_points(g[['x', 'y']]) for _, g in x.groupby('grp2')])

res.head(5)
measurement_id  min  grp
0               0    A      [[(7.435996920897324, 63.64844826366264), (-9....
                1    B      [[(-10.213911323779579, 108.64263032884301), (...
                2    A      [[(6.004534743892181, 38.11898691750269), (12....
                3    B      [[(-11.486905682289555, 68.26172126981378), (-...
                4    A      [[(7.5612638943199295, 28.756743327333556), (-...

res系列的每一行如下:

[[(7.435996920897324, 63.64844826366264),
  (-9.722976872232584, 11.831678494223155),
  (10.809492206072777, 82.9238481225157),
  (-7.918248246978473, 58.46902598333271)],
 [(6.270634566510545, 59.10653240815831),
  (-5.765185730532471, 22.232739287056663),
  (-13.129531349093371, 85.02932179274353)],
 [(0.6686875099768917, 60.634711491838786),
  (-7.373072676442981, 30.897262347426693),
  (-11.489744246260528, 6.834296232736001)]] 

问题是我原来的DataFrame有几百万行,感觉这种解决方案可以从某些优化中受益。

该示例的当前运行时为:

%timeit res = example_df \
    .groupby(['measurement_id', 'min', 'grp']) \
    .apply(lambda x: [df_to_points(g[['x', 'y']]) for _, g in x.groupby('grp2')])
289 ms ± 1.36 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

因此,我的问题是:

  1. numpy多维数组替换元组列表会提高性能吗?
  2. 是否应该避免任何重大瓶颈以提高速度?

@Edit:一个示例,其中grp

定义的组中有各种对象
example_df2 =  pd.DataFrame({'measurement_id': np.concatenate([[0] * 300, [1] * 300]),
                           'min': np.concatenate([np.repeat(range(0, 30), 10), 
                                                  np.repeat(range(0, 30), 10)]),
                           'grp': list(np.repeat(['A', 'B', 'C'], [4, 4, 2])) * 60,
                           'grp2': list(np.random.choice([0, 1, 2], 10)) * 60,
                           'obj': np.array(list(range(0, 10)) * 60),
                           'x': np.random.normal(0.0, 10.0, 600),
                           'y': np.random.normal(50.0, 40.0, 600)})

2 个答案:

答案 0 :(得分:0)

您可以在仅使用array = np.array(df)进行迭代之前将整个数据帧转换为numpy数组。它肯定会提高性能。 您还可以使用多线程模块并行处理并获得性能。 您还可以使用熊猫.apply()代替.iterrows()

答案 1 :(得分:0)

一个小优化是:

def df_to_points(df): 
    return [tuple(x) for x in df.values]

那你得到

In [59]: %timeit res = example_df \ 
    ...:     .groupby(['measurement_id', 'min', 'grp']) \ 
    ...:     .apply(lambda x: [df_to_points(g[['x', 'y']]) for _, g in x.groupby('grp2')])                                        
241 ms ± 14.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

与(原定义为df_to_points)相反

In [58]: %timeit res = example_df \ 
    ...:     .groupby(['measurement_id', 'min', 'grp']) \ 
    ...:     .apply(lambda x: [df_to_points(g[['x', 'y']]) for _, g in x.groupby('grp2')])                                        
284 ms ± 10.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
相关问题