If need test substrings use Series.str.contains:

 conditions = [
        (df['column'].str.contains('one')),
        (df['column'].str.contains('two')),
        (df['column'].str.contains('three')),
        (df['column'].str.contains('other'))] 

If need exact match use Series.eq or ==:

 conditions = [
        (df['column'].eq('one')),
        (df['column'].eq('two')),
        (df['column'].eq('three')),
        (df['column'].eq('other'))] 

 conditions = [
        (df['column'] == 'one'),
        (df['column'] == 'two'),
        (df['column'] == 'three'),
        (df['column'] == 'other')] 

print (new_df)
   column  Int
0     one    1
1    ones    1
2   other    0
3     two    2
4    twos    2
5  others    0
6   three    3
7  threes    3
Answer from jezrael on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.select.html
numpy.select — NumPy v2.5 Manual
>>> import numpy as np · Beginning with an array of integers from 0 to 5 (inclusive), elements less than 3 are negated, elements greater than 3 are squared, and elements not meeting either of these conditions (exactly 3) are replaced with a default value of 42. >>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [-x, x**2] >>> np.select(condlist, choicelist, 42) array([ 0, -1, -2, 42, 16, 25]) When multiple conditions are satisfied, the first one encountered in condlist is used.
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-select-function-python
numpy.select() function - Python - GeeksforGeeks
July 12, 2025 - The numpy.select() function is used to construct an array by selecting elements from a list of choices based on multiple conditions.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.select.html
numpy.select — NumPy v2.1 Manual
>>> import numpy as np · Beginning with an array of integers from 0 to 5 (inclusive), elements less than 3 are negated, elements greater than 3 are squared, and elements not meeting either of these conditions (exactly 3) are replaced with a default value of 42. >>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist, 42) array([ 0, 1, 2, 42, 16, 25]) When multiple conditions are satisfied, the first one encountered in condlist is used.
Top answer
1 of 5
34

We can use np.core.defchararray.find to find the position of foo string in each element of bar, which would return -1 if not found. Thus, it could be used to detect whether foo is present in each element or not by checking for -1 on the output from find. Finally, we would use np.flatnonzero to get the indices of matches. So, we would have an implementation, like so -

np.flatnonzero(np.core.defchararray.find(bar,foo)!=-1)

Sample run -

In [91]: bar
Out[91]: 
array(['aaa', 'aab', 'aca'], 
      dtype='|S3')

In [92]: foo
Out[92]: 'aa'

In [93]: np.flatnonzero(np.core.defchararray.find(bar,foo)!=-1)
Out[93]: array([0, 1])

In [94]: bar[2] = 'jaa'

In [95]: np.flatnonzero(np.core.defchararray.find(bar,foo)!=-1)
Out[95]: array([0, 1, 2])
2 of 5
6

Look at some examples of using in:

In [19]: bar = np.array(["aaa", "aab", "aca"])

In [20]: 'aa' in bar
Out[20]: False

In [21]: 'aaa' in bar
Out[21]: True

In [22]: 'aab' in bar
Out[22]: True

In [23]: 'aab' in list(bar) 

It looks like in when used with an array works as though the array was a list. ndarray does have a __contains__ method, so in works, but it is probably simple.

But in any case, note that in alist does not check for substrings. The strings __contains__ does the substring test, but I don't know any builtin class that propagates the test down to the component strings.

As Divakar shows there is a collection of numpy functions that applies string methods to individual elements of an array.

In [42]: np.char.find(bar, 'aa')
Out[42]: array([ 0,  0, -1])

Docstring:
This module contains a set of functions for vectorized string operations and methods. The preferred alias for defchararray is numpy.char.

For operations like this I think the np.char speeds are about same as with:

In [49]: np.frompyfunc(lambda x: x.find('aa'), 1, 1)(bar)
Out[49]: array([0, 0, -1], dtype=object)

In [50]: np.frompyfunc(lambda x: 'aa' in x, 1, 1)(bar)
Out[50]: array([True, True, False], dtype=object)

Further tests suggest that the ndarray __contains__ operates on the flat version of the array - that is, shape doesn't affect its behavior.

🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.select.html
numpy.select — NumPy v2.6.dev0 Manual
>>> import numpy as np · Beginning with an array of integers from 0 to 5 (inclusive), elements less than 3 are negated, elements greater than 3 are squared, and elements not meeting either of these conditions (exactly 3) are replaced with a default value of 42. >>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [-x, x**2] >>> np.select(condlist, choicelist, 42) array([ 0, -1, -2, 42, 16, 25]) When multiple conditions are satisfied, the first one encountered in condlist is used.
Top answer
1 of 2
1

Create a mapping of each country -> area, then use explode and map to apply the mapping and finally, use groupby and apply to rebuild the list:

geo = {'Europe': Europe, 'North_America': North_America, 'Asia': Asia}
mapping = {country: area for area, countries in geo.items() for country in countries}

df['geo'] = df['location'].str.split(', ').explode().map(mapping) \
                          .groupby(level=0).apply(', '.join)

Output:

>>> df
            location                    geo
0      London, Paris         Europe, Europe
1          Hong Kong                   Asia
2   London, New York  Europe, North_America
3  Singapore, Toroto    Asia, North_America
4             Boston          North_America
2 of 2
1

By using NumPy library together with python for loops we can get the results. At first we combine lists of country's cities together and then create another list named continents which length is the same as the created list of cities:

import numpy as np
import pandas as pd

continents = ["Europe"] * len(Europe) + ["North_America"] * len(North_America) + ["Asia"] * len(Asia)
countries = Europe + North_America + Asia
locations = data['location']

Then for each city, even for each in the combinations, we find its index in the created country list. Then we create a list for number of commas in each of that combinations for using to create the desired output with commas:

corsp = []
comma_nums = []
for i in locations:
    for j, k in enumerate(i.split(', ')):
        corsp.append(np.where(np.array(countries) == k)[0][0])
    comma_nums.append(j)

continents list will be reordered and modified by created index list. Then its arguments combined in list format as the combination style which where in locations, and then the lists convert to strings as they are needed for the output:

reordered_continents = [continents[i] for i in corsp]

mod_continents = []
iter = 0
f = 1
for i in comma_nums:
    mod_continents.append(reordered_continents[iter:i + f])
    iter = i + f
    f = iter + 1

for i, j in enumerate(mod_continents):
    if len(j) > 1:
        for k in j:
            mod_continents[i] = ', '.join(j)
    else:
        mod_continents[i] = ''.join(j)

df['geo'] = mod_continents
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.select.html
numpy.select — NumPy v2.4 Manual
>>> import numpy as np · Beginning with an array of integers from 0 to 5 (inclusive), elements less than 3 are negated, elements greater than 3 are squared, and elements not meeting either of these conditions (exactly 3) are replaced with a default value of 42. >>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [-x, x**2] >>> np.select(condlist, choicelist, 42) array([ 0, -1, -2, 42, 16, 25]) When multiple conditions are satisfied, the first one encountered in condlist is used.
🌐
NumPy
numpy.org › doc › 1.22 › reference › generated › numpy.select.html
numpy.select — NumPy v1.22 Manual
>>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist, 42) array([ 0, 1, 2, 42, 16, 25])
Find elsewhere
🌐
NumPy
numpy.org › doc › 1.18 › reference › generated › numpy.select.html
numpy.select — NumPy v1.18 Manual
>>> x = np.arange(10) >>> condlist = [x<3, x>5] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist) array([ 0, 1, 2, ..., 49, 64, 81])
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.select.html
numpy.select — NumPy v2.0 Manual
>>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist, 42) array([ 0, 1, 2, 42, 16, 25])
🌐
KajoData
kajodata.com › knowledge base › python › how numpy select works in python? best example
How numpy select works in Python? Best example - KajoData
The numpy.select() function allows you to define a list of conditions and corresponding choices. It evaluates each condition and assigns a value from the respective choice list wherever the condition holds true.
🌐
SciPy
docs.scipy.org › doc › › numpy-1.10.4 › reference › generated › numpy.select.html
numpy.select — NumPy v1.10 Manual
numpy.select(condlist, choicelist, default=0)[source]¶ · Return an array drawn from elements in choicelist, depending on conditions. See also · where · Return elements from one of two arrays depending on condition. take, choose, compress, diag, diagonal ·
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.select.html
numpy.select — NumPy v2.3 Manual
>>> import numpy as np · Beginning with an array of integers from 0 to 5 (inclusive), elements less than 3 are negated, elements greater than 3 are squared, and elements not meeting either of these conditions (exactly 3) are replaced with a default value of 42. >>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [-x, x**2] >>> np.select(condlist, choicelist, 42) array([ 0, -1, -2, 42, 16, 25]) When multiple conditions are satisfied, the first one encountered in condlist is used.
🌐
Medium
medium.com › @amit25173 › how-numpy-select-works-f11ed0de629b
How numpy.select() Works?. If you think you need to spend $2,000… | by Amit Yadav | Medium
February 8, 2025 - It's a super-efficient function that lets you apply multiple conditions to your array and select corresponding values based on those conditions—much faster than a traditional loop.
🌐
NumPy
numpy.org › doc › 1.16 › reference › generated › numpy.select.html
numpy.select — NumPy v1.16 Manual
February 18, 2020 - numpy.select(condlist, choicelist, default=0)[source]¶ · Return an array drawn from elements in choicelist, depending on conditions. See also · where · Return elements from one of two arrays depending on condition. take, choose, compress, diag, diagonal ·
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.choose.html
numpy.choose — NumPy v2.6.dev0 Manual
>>> import numpy as np >>> choices = [[0, 1, 2, 3], [10, 11, 12, 13], ... [20, 21, 22, 23], [30, 31, 32, 33]] >>> np.choose([2, 3, 1, 0], choices ... # the first element of the result will be the first element of the ... # third (2+1) "array" in choices, namely, 20; the second element ...