比itertuples内的loc更快地更新列的块
我有以下代码,它遍历数据帧并根据其他两个列更新列的块。当前的解决方案使用locinside itertuples.
是否可以在不诉诸手动并行化或拆分数据帧的情况下使代码更快?
n_rows = 10000
ix_ = pd.date_range(start="2020-01-01 00:00", freq="min", periods=n_rows)
offsets_ = pd.to_timedelta(np.random.randint(0, 60, size=n_rows), unit="min")
df = pd.DataFrame(
ix_ + pd.to_timedelta(offsets_, unit="min"), index=ix_, columns=["t_end"]
)
df["active"] = 0
for row in df.itertuples():
df.loc[row.Index : row.t_end, "active"] += 1