我正在尝试使用 iloc 替换 Pandas 数据帧的最后一行,但是我无法让它工作。有很多解决方案,但最简单(最慢)的是这里:
如何在 Python 中对 Pandas 数据帧上的行进行 FIFO 推送操作? https://stackoverflow.com/questions/41360265/how-to-do-a-fifo-push-operation-for-rows-on-pandas-dataframe-in-python
为什么这个方法在下面的代码中不起作用?
def append_from_dataframe(self,timeframe,new_dataframe):
new_dataframe.reset_index(inplace=True)
temp_dataframe = self.timeframedict.get(timeframe)
num_rows_existing = temp_dataframe.shape[0]
num_rows_new = new_dataframe.shape[0]
overlap = (num_rows_existing + num_rows_new) - 500
# slow, replace with numpy array eventually
if overlap >= 1:
# number of rows to shift
i = overlap * -1
#shift the dataframe back in time
temp_dataframe = temp_dataframe.shift(i)
#self.timeframedict.get(timeframe) = self.timeframedict.get(timeframe).shift(overlap)
#replace the last i rows with the new values
temp_dataframe.iloc[i:] = new_dataframe
self.timeframedict.update({timeframe:temp_dataframe})
else:
#TODO - see this https://stackoverflow.com/questions/10715965/add-one-row-in-a-pandas-dataframe
self.timeframedict.update({timeframe:self.timeframedict.get(timeframe).append(new_dataframe)})
数据框的内容替换另一行中的一行:
ipdb> new_dataframe
Timestamp Open High Low Close Volume localtime
0 1533174420000 423.43 423.44 423.43 423.44 0.73765 1533174423776
temp_dataframe.shift(i)
将值移回 1,用 NaN 替换值 -
ipdb> temp_dataframe.iloc[i:]
Timestamp Open High Low Close Volume localtime
499 NaN NaN NaN NaN NaN NaN NaN
However temp_dataframe.iloc[i:] = new_dataframe
不取代任何东西。
编辑:我应该补充一点,经过更多的尝试后,我可以将 1 行替换为:
temp_dataframe.iloc[-1] = new_dataframe.iloc[0]
但是,我无法让多行版本工作