0

I love vectorization and I have following df

df = pd.DataFrame({'p1':['apple','orange'],
                   'p1_dog':['True', 'False'],
                   'p2':['quick','start'],
                   'p2_dog':['True', 'True'],
                   'p3':['ash','sword'],
                   'p3_dog':['False','False']})

Trying to create a new column with values equal p1 or p2 or p3 depends on values in p1_dog and p2_dog and p3_dog.

Using this code:

df['final'] = 0
df['final'] = [[(p1 if p1_dog == p2_dog == p3_dog == True)\
                     | (p2 if (p1_dog == False) &  (p2_dog == p3_dog == True)\
                        |(p3 if (p1_dog == p2_dog == False) & (p3_dog == True))) for x in df['final']]]  

Though it doesn't work... Please help - where is my mistake?

It_is_Chris
  • 13,504
  • 2
  • 23
  • 41
AlexR
  • 9
  • 1
  • 6

1 Answers1

0

The working version of the answer given by mortysporty... Thanks again, comrade! Just a little enhanced the booleans

def evaluate(p1, p2, p3, p1_dog, p2_dog, p3_dog):

if (p1_dog and p2_dog and p3_dog) or (p1_dog and p2_dog) or (p1_dog and p3_dog) or (p1_dog):
    return p1
elif (p2_dog and p3_dog) or (p2_dog):
    # If you are getting here... p1_dog must be False
    return p2
elif p3_dog:
    # ...same here. p1_dog and p2_dog must be False
    return p3
else:
    return "I dont know what you want to happen here"

a = pd.DataFrame({'p1':['apple','orange', 'ball'],
               'p1_dog':[True, False, False],
               'p2':['quick','start', 'heck'],
               'p2_dog':[False, True, True],
               'p3':['ash','sword', 'soop'],
               'p3_dog':[True, False, True]})

a['final'] = [evaluate(*p) for p in zip(a['p1'], a['p2'], a['p3'],
               a['p1_dog'], a['p2_dog'], a['p3_dog'])]
AlexR
  • 9
  • 1
  • 6