当前位置: 代码迷 >> python >> 如何比较熊猫中的列并使用是或否创建新列
  详细解决方案

如何比较熊猫中的列并使用是或否创建新列

热度:25   发布时间:2023-06-13 14:15:44.0
    A           B           C           D
0   0.397333    Xor         0.569748    0.406415
1   0.319684    x           0.159117    0.522648
2   0.778038    0.486989    x           x
3   0.549993    0.896913    0.960814    0.430113
4   0.251655    0.802137    Xand        0.218265

在这里,我需要比较所有四列,并且需要有一个新的column E ,我将在其中获得新的信息。

我需要检查是否四列中的任何一个包含xColumn EYes else No

输出量

    A           B           C           D          E
0   0.397333    Xor         0.569748    0.406415   No
1   0.319684    x           0.159117    0.522648   Yes
2   0.778038    0.486989    x           x          Yes
3   0.549993    0.896913    0.960814    0.430113   No
4   x           0.802137    Xand        0.218265   Yes

我想在这里使用where子句,但我无法做到这一点,而lambda也无法理解应如何编写。

这是我的代码:

def YorN(stri):
    if stri =='x':
        return True
    else:
        return False

df['E'] = np.where(YorN(df.B) | YorN(df.C) | YorN(df.D)| YorN(df.A), 'Yes', 'No')

错误:

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

编辑1我的其他列可能包含其他一些变量

如您所知,您的比较函子无法正常工作,您正在尝试将标量与数组进行比较。 无论如何,您可以调用apply并传递axis=1来逐行处理df。 str.contains转换为str以便可以str.contains量化的str.containsany一起使用以生成布尔序列,并将其用作np.where的arg,并分别在TrueFalse时返回“ yes”或“ no”:

In [8]:
df['E'] = np.where(df.astype(str).apply(lambda x: x.str.contains('x').any(), axis=1), 'yes', 'no')
df

Out[8]:
          A         B         C         D    E
0  0.397333  0.245596  0.569748  0.406415   no
1  0.319684         x  0.159117  0.522648  yes
2  0.778038  0.486989         x         x  yes
3  0.549993  0.896913  0.960814  0.430113   no
4  0.251655  0.802137  0.024341  0.218265   no

编辑

答案仍然有效:

In [10]:
df['E'] = np.where(df.astype(str).apply(lambda x: x.str.contains('x').any(), axis=1), 'yes', 'no')
df

Out[10]:
          A         B         C         D    E
0  0.397333       Xor  0.569748  0.406415   no
1  0.319684         x  0.159117  0.522648  yes
2  0.778038  0.486989         x         x  yes
3  0.549993  0.896913  0.960814  0.430113   no
4  0.251655  0.802137      Xand  0.218265   no
  相关解决方案