Pandas : 比较超过 3 列失败

Pandas : Comparing more than 3 columns fails

在 pandas 中,我只想比较总共 8 列中的 3 列(按名称选择),并获得“结果”。

# Columns to compare are  ::  ColB, ColD and ColF


Fruits  ColA    ColB    ColC    ColD    ColE    ColF    Outcome
Loquat  83  98  91  98  78  96  FALSE
Medlar  82  94  87  94  91  94  TRUE
Pear    77  74  79  71  79  71  FALSE
Quince  71  93  78  93  92  93  TRUE
Date    98  81  73  94  97  99  FALSE
Rowan   89  85  77  85  95  85  TRUE
Lime    97  91  71  90  88  85  FALSE

是否有任何代码可以帮助我一次比较 2 个以上的列并获得布尔值? (我知道比较 2 列适用于下面的代码,但如果我添加第三列,它会在最后显示错误)

#  I have tried the below code:

df.loc[(df['ColB']==df['ColD']==df['ColF']), 'Outcome'] = "True"

Traceback (most recent call last):

File "C:\Py378\Tests\Trial.py", line 15, in <module>
    df.loc[(df['ColB']==df['ColD']==df['ColF']), 'Outcome'] = "True"

  File "c:\py378\py\lib\site-packages\pandas\core\generic.py", line 1479, in __nonzero__
    f"The truth value of a {type(self).__name__} is ambiguous. "

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

如果我从中删除“==df['ColF']”,上面的内容会起作用,所以我知道比较 2 列是有效的...是否有任何格式可以按名称添加列(超过 3 到 5 个)它会起作用吗?

试试这个:

df.loc[(df['ColB']==df['ColD']) & (df['ColD']==df['ColF']), 'Outcome'] = "True"

让我们尝试 DataFrame.eqaxis=0:

x, *y = ['ColB', 'ColD', 'ColF']
df['Outcome'] = df[y].eq(df[x], axis=0).all(1)

# df['Outcome']

0    False
1     True
2    False
3     True
4    False
5     True
6    False
Name: Outcome, dtype: bool