我的数据框具有以下值

        Bird    Color
   0    Parrot  ['Light_Blue','Green','Dark_Blue']
   1    Eagle   ['Sky_Blue','Black','White', 'Yellow','Gray']
   2    Seagull ['White','Jet_Blue','Pink', 'Tan','Brown', 'Purple']


我想创建一个名为“ No Blue”的列,该列将仅列出其中没有单词“ Blue”的数组元素。

像这样:

    Bird    Color                                                No Blue
0   Parrot  ['Light_Blue','Green','Dark_Blue']                   ['Green']
1   Eagle   ['Sky_Blue','Black','White', 'Yellow','Gray']        ['Black', 'White', 'Yellow', 'Gray']
2   Seagull ['White','Jet_Blue','Pink', 'Tan','Brown', 'Purple'] ['White', 'Pink', 'Tan', 'Brown', 'Purple']


这是我最接近解决方案的事情

>>> Eagle = ['Sky_Blue','Black','White', 'Yellow','Gray']
>>> matching = [x for x in Eagle if "Blue" not in x]
>>> matching
['Black', 'White', 'Yellow', 'Gray']

最佳答案

我将使用以下代码:

df["noBlue"]=df.Color.apply(lambda x: [v for v in x if "Blue" not in v])

关于python - 如何基于pandas或numpy中的条件从数组中提取值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57509388/

10-11 19:33