编辑:
我正在尝试模拟在线决策过程。在每次迭代中,我希望从已知的数据帧中读取新的行,并根据它做出决定。另外,我想保存我使用的数据帧的最后n行。不幸的是,即使是遍历各行也非常慢。
我怎样才能做得更好呢?
import pandas as pd
import numpy as np
import time
t0 = time.time()
s1 = np.random.randn(2000000)
s2 = np.random.randn(2000000)
time_series = pd.DataFrame({'s1': s1, 's2': s2})
n = time_series.shape[0]
for t in range(1, n - 1):
curr_data = time_series.iloc[t]
print time.time() - t0
旧版本:
我有一个循环,在每次迭代中,我需要删除数据帧的第一行,并将另一行追加到末尾。使用最快的方法是什么?
发布于 2018-01-03 15:42:18
如果确实需要,可以使用:
for i in range(3):
#remove first row
df = df.iloc[1:]
#e.g. append second row
row = df.iloc[1]
#append new row
df.loc[len(df.index)] = row
但如果选择this post,这是最糟糕的解决方案:
6)更新空帧(例如,使用loc at-at-time的)
所以我猜这里应该有更好/更快的解决方案。第一步是尽可能避免循环。
https://stackoverflow.com/questions/48073021
复制相似问题