-1

I have a pandas dataframe with the three columns names A, B, C and want to add a fourth column with the following condition:

Note: prev X = previous entry of the column X

D = IF(A < prev D) and (B > prev D)

THEN A

ELSE prev D

Is there a way other than using for loop for accomplishing this using pandas or numpy?

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252

1 Answers1

1
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=list('ABCD'))
df['E'] = np.where((df['A'] < df['D'].shift(1)) & 
                   (df['B'] > df['D'].shift(1)), df['A'], df['D'].shift(1))

Obviously this will make the first row always return the value of NaN instead of A or D previous value since the first value in df['D'].shift(1) will be NaN

Matthew Barlowe
  • 2,229
  • 1
  • 14
  • 24