[英]How to create a new column based on row value in previous row in Pandas dataframe?
I want to add a column which encodes a 1 if the Adjusted Closing Price of a stock at row T went up compared to the Adjusted Closing Price at row T-1, and encode a 0 if it went down instead.我想添加一个列,如果 T 行的股票的调整收盘价与 T-1 行的调整收盘价相比上涨,则编码为 1,如果它下跌,则编码为 0。
The dataframe looks like: dataframe 看起来像:
Eg, at row index 1298 for the new column should be 0例如,新列的行索引 1298 处应为 0
What's the best possible way to get this done (eg, via np.where()?) Any input is appreciated.完成这项工作的最佳方法是什么(例如,通过 np.where()?)任何输入都值得赞赏。
You can first sort_values
on your date column to make sure they are in the right order to perform the comparison, and then you can use np.where
with shift()
to compare the previous value in Adj Close with the current one:您可以首先对日期列进行
sort_values
以确保它们以正确的顺序执行比较,然后您可以使用np.where
和shift()
将 Adj Close 中的前一个值与当前值进行比较:
# Sort by date
df.sort_values(by='Date',ascending=True)
# Create a column comparing previous Adj Close with current Adj Close
import numpy as np
df['i'] = np.where(df['Adj Close'].shift(1) < df['Adj Close'],1,0)
df
Date Open High ... Adj Close Volume i
index ...
1297 2021-03-01 104.540001 133.990005 ... 120.40 49597300 0
1298 2021-03-02 116.930000 133.199900 ... 118.18 33640400 0
1299 2021-03-03 122.500000 127.700000 ... 124.18 19173700 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.