0

I am trying to generate a pandas dataframe column from a logical operation between elements of three other columns. I know I can slog through it row by row but I am sure there is a neater way to implement this. Below is what I would do if standard operations worked between dataframe columns (obviously this code does not work). The operation relies on accessing the previous value in that column, hence the .shift() in the last line.

if dataframe['C'] > dataframe['H']:
    dataframe['Result'] = 1
else:
    if dataframe['C'] < dataframe['L']:
        dataframe['Result'] = -1
    else:
        dataframe['Result'] = dataframe['Result'].shift()
alex.l
  • 29
  • 5
  • 1
    Please post a sample dataframe along with the desired outcome; then it is easier to help. – Cleb Feb 27 '21 at 20:14

1 Answers1

0

You're result can probably achieved by using .loc[] and masks:

df = pd.DataFrame([[10,2,3], [4,5,6], [7,80,9]], columns=['C', 'H', 'L'])
df['result'] = pd.NA
df.loc[df.C > df.H, 'result'] = 1
df.loc[(df.C <= df.H) & (df.C < df.L), 'result'] = -1
df.result.fillna(method='bfill')

You convert your conditions into masks and subset your data frame using those masks.

I didn't completely understand why you want to use .shift(). If it's only to copy the value of the last non-empty row, you might as well use .fillna(method='bfill').

If you want to perform some other operation with those rows, I'd suggest to create an appropriate mask.

See also the documentation on pandas masks.

nehalem
  • 397
  • 2
  • 20