简体   繁体   中英

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:

在此处输入图像描述

Eg, at row index 1298 for the new column should be 0

What's the best possible way to get this done (eg, via np.where()?) Any input is appreciated.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM