[英]Replace a column starting from a certain row
Consider this example:考虑这个例子:
rates_frame = rates_frame[['new_date', 'new_time', 'open', 'high', 'low', 'close', 'tick_volume']]
rates_frame
rates_frame.head()
Which produces this output:产生这个输出:
I want to replace values in column open
starting from row 1 by values from column close
starting from row 0.我想用从第 0 行开始的列
close
值替换从第 1 行开始的列open
的值。
You are looking for the shift
function in pandas
.您正在寻找
pandas
的shift
函数。 This shifts the values in the particular column by a specified number of periods这会将特定列中的值移动指定的周期数
All you need is this你只需要这个
rates_frame['open_new'] = rates_frame['close'].shift(1)
PS : From the perspective of the share market, I don't think you should do this. PS :从股票市场的角度来看,我认为你不应该这样做。 It's not necessary that the opening price of today is equal to the closing price of the previous day.
今天的开盘价不必等于前一天的收盘价。 You can look into topics like gap-up opening, gap-down opening and unchanged.
您可以查看间隙开盘、间隙开盘和不变等主题。 Only in the latter case, what you are doing holds true.
只有在后一种情况下,您所做的才是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.