我有一个有一百万行的数据帧。我有一个单独的函数(我不能向量化)来应用到每一行。我研究了swifter,它承诺利用多个进程来加速计算。然而,在8核机器上,情况并非如此。
知道为什么吗?
def parse_row(n_print=None):
def f(row):
if n_print is not None and row.name % n_print == 0:
print(row.name, end="\r")
return Feature(
geometry=Point((float(row["longitude"]), float(row["latitude"]))),
properties={
"water_level": float(row["water_level"]),
"return_period": float(row["return_period"])
}
)
return f
In [12]: df["feature"] = df.swifter.apply(parse_row(), axis=1)
Dask Apply: 100%|████████████████████████████████████████| 48/48 [01:19<00:00, 1.65s/it]
In [13]: t = time(); df["feature"] = df.apply(parse_row(), axis=1); print(int(time() - t))
46发布于 2021-05-03 16:29:47
这主要取决于所涉及的处理能力以及矢量化/并行处理/优化是否可以改善问题。有时这根本不是一个解决方案。还要记住,swifter需要时间来计算它的预计工作时间跨度,有时df.apply会更快,因为它不需要计算,优化可能也没有帮助。
https://stackoverflow.com/questions/58310509
复制相似问题