我有一个数据集,其结构如下所示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)]]
问题是,我原来的数据帧有几百万行,感觉这个解决方案可以从一些优化中受益。你知道吗
示例的当前运行时为:
%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)
因此,我的问题是:
numpy
多维数组替换元组列表会提高性能吗?你知道吗@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)})
在使用
array = np.array(df)
迭代之前,可以将整个数据帧转换为numpy数组。它一定会提高性能。 您还可以使用多线程模块来并行处理并获得性能。 您也可以使用熊猫.apply()
,而不是使用.iterrows()
可以使用
.pivot_table()
和aggfunc=
简单list
:印刷品:
使用
timeit
进行基准测试:印刷品:
编辑:通过更新问题,您可以执行以下操作:
基准:
印刷品:
一个轻微的优化是:
然后你得到
与之相反(使用
df_to_points
的原始定义)相关问题 更多 >
编程相关推荐