1

I am trying to achieve the following using python pandas but not able to figure out why it's not working. I have searched extensively in the web but desired outcome unable to find.

Data is as follows

Data    ADJ_LOW_1   ADJ_LOW_2       MIN     ADJ_HIGH_1     ADJ_HIGH_1   MAX
Data1   34.155      33.81       33.3    36.865     36.865   36.9
Data2   15.444      15.288      15.9    15.756     15.756   17.25
Data3   2.3265      2.303       2.35    2.4745     2.4745   2.6
Data4   21938.4     21716.8     21242   22624      22624    22600
Data5   6.5835      6.517       6.25    6.8175     6.8175   7.45
Data6   196.02      194.04      192.15  218.16     218.16   214.4
Data7   109.395     108.29      107.95  126.654    126.654  113.6
Data8   7.7715      7.693       7.5         8.383      8.383    8.25
Data9   604.89      598.78      572         640.3905   640.3905 648.9
Data10  843.5295    835.009     829         871.327    871.327  924.25

data type is

ADJ_LOW_1     float64
ADJ_LOW_2     float64
MIN           float64
ADJ_HIGH_1    float64
ADJ_HIGH_1    float64
MAX           float64

dtype: object

my objective is to achieve the following outcome:

if ADJ_LOW_1 <= MIN & ADJ_HIGH_1 >= MAX then insert column 'Result' and put value "P1"

elif

if ADJ_LOW_2<= MIN & ADJ_HIGH_1 >= MAX then insert column 'Result' and put value "P2"

elif

if ADJ_LOW_1<= MIN & ADJ_HIGH_2 >= MAX then insert column 'Result' and put value "P3"

esle

  "Blank"
``
x is the data reference 
`x.ADJ_LOW_1<=x.MIN & x.ADJ_HIGH_1>=x.MAX: x['R']="P1" elif x.ADJ_LOW_2<=x.MIN & x.ADJ_HIGH_1>=x.MAX: x['R']="P2" elif x.ADJ_LOW_1<=x.MIN & x.ADJ_HIGH_2>=x.MAX: x['R']="P3" else: x['R']=" "`
  • 1
    where is your original code? – Umar.H Jan 19 '20 at 18:56
  • here it is. x is the df if x.ADJ_LOW_1<=x.MIN & x.ADJ_HIGH_1>=x.MAX: x['R']="P1" elif x.ADJ_LOW_2<=x.MIN & x.ADJ_HIGH_1>=x.MAX: x['R']="P2" elif x.ADJ_LOW_1<=x.MIN & x.ADJ_HIGH_2>=x.MAX: x['R']="P3" else: x['R']=" " x – Shankha Karmakar Jan 19 '20 at 18:58
  • @ShankhaKarmakar Please don't share that kind of information in the comments, it's difficult to find and read. Instead, make an edit to your question. – AMC Jan 19 '20 at 21:47
  • _I am trying to achieve the following using python pandas but not able to figure out why it's not working._ What are you trying, exactly? I can't see any code, and this seems like basic Pandas. Have you read the Pandas docs? As an aside, I would really encourage you to use `[ ]` for column access, instead of the `.`/attribute style. – AMC Jan 19 '20 at 21:48
  • Oh, and this is a duplicate of https://stackoverflow.com/q/19913659/11301900. – AMC Jan 19 '20 at 21:49

1 Answers1

2

You can use np.select for handling multiple conditions:

conditions = [
    (df['ADJ_LOW_1'] <= df["MIN"]) & (df['ADJ_HIGH_1'] >= df['MAX']),
    (df['ADJ_LOW_2'] <= df["MIN"]) & (df['ADJ_HIGH_1'] >= df['MAX']),
    (df['ADJ_LOW_1'] <= df["MIN"]) & (df['ADJ_HIGH_2'] >= df['MAX'])]

choices = ['P1', 'P2', 'P3']

df['Result'] = np.select(conditions, choices, default='Blank')
YOLO
  • 20,181
  • 5
  • 20
  • 40