Reputation: 173
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.
The dataframe looks like:
E.g., at row index 1298 for the new column should be 0
What's the best possible way to get this done (e.g., via np.where()?) Any input is appreciated.
Upvotes: 1
Views: 2792
Reputation: 13821
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 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
Upvotes: 1