Viol1997
Viol1997

Reputation: 173

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.

The dataframe looks like:

enter image description here

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

Answers (1)

sophocles
sophocles

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

Related Questions