熊猫矢量化操作中的多个布尔条件

时间:2019-03-08 18:29:17

标签: python pandas vectorization

我喜欢矢量化,并且关注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']})

尝试创建值等于p1或p2或p3的新列取决于p1_dog和p2_dog和p3_dog中的值。

使用此代码:

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']]]  

尽管它不起作用...请帮助-我的错误在哪里?

2 个答案:

答案 0 :(得分:0)

我认为也许我会尝试将条件分解为单独的功能。它变得越来越复杂..

也许是这样...我不确定我是否精确确定了你的条件

import pandas as pd

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

    if p1_dog and p2_dog and p3_dog:
        return p1
    elif p2_dog and p3_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"

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

df['final'] = [evaluate(*p) for p in zip(df['p1'], df['p2'], df['p3'],
               df['p1_dog'], df['p2_dog'], df['p3_dog'])]

如果您不想使用单独的功能,请查看此帖子`elif` in list comprehension conditionals,以获取有关如何在列表理解中使用if-elif-else的提示。

我还注意到在您的数据框定义中,您编写了布尔值(True,  False)放在方括号中。我猜这是一个错误。我编写的函数只有在不放在方括号中时才起作用,即不是字符串而是实际的布尔值。

答案 1 :(得分:0)

mortysporty给出的答案的工作版本...再次感谢同志! 只是增强了布尔值

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'])]