sdp
sdp

Reputation: 11

Modify values in 1 column based on the value in another column in the previous row

I have a dataframe with 3 columns and 4 row,

import pandas as pd
df_ticker = pd.DataFrame({'Min_val': [22382,37876,46717,62247], 'Max_val': [36901,46716,62045,182727],
               'Ticker':['$','$$','$$$','$$$$']})
df_ticker

Sample input:

enter image description here

I want to modify Min value in the dataframe so than at index 1 min_val=max_val(at index 0) + 1.

Output:

enter image description here

Please let me know how I can achieve this.

Upvotes: 0

Views: 40

Answers (1)

ALollz
ALollz

Reputation: 59529

The shift function works. To keep the Min_val in the first column you can just specify 1: with the loc function, otherwise it would set the first row to NaN

df_ticker.loc[1:,'Min_val'] = df_ticker['Max_val'].shift(1) + 1

Upvotes: 1

Related Questions