我想知道是否有人可以提供一些关于提高熊猫结果的速度和计算的意见。
我想要获得的是基于第二个表(UUID)的每一行的一个表(玩家表)中的 ID 总和。从功能上讲,每一行都需要对其 Active 行中包含的 player 表行的总数求和,并将 UUID 分配为该行的索引。
我最初的想法是逐行循环并计算出我的结果,但这产生了相当缓慢的结果,我怀疑这不是可以完成的最佳方式。在下面的版本中,我估计完整数据集的总时间约为 66 分钟。在 10,000 个子样本上运行大约需要 20 秒。
有人有更好的解决方案来计算这些结果吗?
提前致谢!
UUID 表
这是整个表的一个子集
形状 = (2060590, 2)
玩家 ID 表
这是整个表的一个子集
形状 = (39,8)
决赛桌
代码
# executes in ~20 seconds
df = None
for ix, i in enumerate(uuid_df[["UUID", "Active"]].sample(10000).itertuples(index=False)):
# Get UUID for row
_uuid = i[0]
# Get list of "Active items" (these are the ones that will be summed)
_active = i[1]
# Create new frame by selecting everything from points table where the ID is in the Active List.
# Sum selected values, convert to a dataframe with UUID as index and tranpose
_dff = points_table_df.loc[points_table_df.index.isin(_active)].sum().to_frame(_uuid).T
# Check if first dataframe, if not concat to existing one
if df is None:
df = _dff
else:
df = pd.concat([df, _dff])


