luke_t
luke_t

Reputation: 2985

numpy.where with like operator

I would like to use np.where, but need to match a string using a wildcard. Is this possible, or is there another function that is best to be used in this scenario?

df['PRODUCT'] = np.where(df['TYPE'] == '2b', 'Pencil',
                np.where(df['TYPE'] like 'I5%', 'Ruler', 0))

I tried using the in operator, however this did not work.

df['PRODUCT'] = np.where(df['TYPE'] == '2b', 'Pencil',
                np.where('I5' in df['TYPE'], 'Ruler', 0))

Upvotes: 2

Views: 8022

Answers (1)

jezrael
jezrael

Reputation: 863166

You need contains:

df['PRODUCT'] = np.where(df['TYPE'] == '2b', 'Pencil',
                np.where(df['TYPE'].str.contains('I5'), 'Ruler', 0))

Sample:

df = pd.DataFrame({'TYPE':['2b','2c','I5','I5 a', 'a I5']})
print (df)
   TYPE
0    2b
1    2c
2    I5
3  I5 a
4  a I5

df['PRODUCT'] = np.where(df['TYPE'] == '2b', 'Pencil',
                np.where(df['TYPE'].str.contains('I5'), 'Ruler', 0))

print (df)
   TYPE PRODUCT
0    2b  Pencil
1    2c       0
2    I5   Ruler
3  I5 a   Ruler
4  a I5   Ruler

If need check only starts of string add ^:

df['PRODUCT'] = np.where(df['TYPE'] == '2b', 'Pencil',
                np.where(df['TYPE'].str.contains('^I5'), 'Ruler', 0))

print (df)
   TYPE PRODUCT
0    2b  Pencil
1    2c       0
2    I5   Ruler
3  I5 a   Ruler
4  a I5       0

Upvotes: 7

Related Questions