Add a new column to a pandas dataframe if the corresponding item in the previous column is in a list

Viewed 30

I have a dataframe which looks similar to the following:

    X       Y
0  21    left_hey
1  43   hello
2  54  center
3  12     you front
4  64    jack
5  76     roy
6  42    back

And a list:

words = ['center', 'back', 'right', 'left', 'front']

MRE for dataframe:

df = pd.DataFrame({
    'X': [21, 43, 54, 12, 64, 76, 42],
    'Y': ['left_hey', 'hello', 'center', 'you front', 'jack', 'roy', 'back']
})

Now I want to insert another column in df (say Z). The elements in Z would depend on the corresponding element in Y. If the substring in Y matches any word in words insert 1 in Z and 0 otherwise.

So my expected output should look like:

    X           Y  Z
0  21  left hello  1
1  43       hello  0
2  54      center  1
3  12   you front  1
4  64        jack  0
5  76         roy  0
6  42        back  1

Now I know that I could use the method Series.str.contains(word), but I don't seem to figure out how to pass all the elements from words at the same time and match the substring with Y, I've also tried to use any() but it returns boolean.

Also I could loop through words and explicitly match each word but that would be really slow if the dataframe is large (as my use-case).

Thanks in advance for any suggestions:)

1 Answers

Use Series.isin for boolean mask and then are 3 ways for mapping True/False to 1/0:

df['Z'] = df.Y.isin(words).astype(int)
df['Z'] = df.Y.isin(words).view('i1')
df['Z'] = np.where(df.Y.isin(words),1,0)

EDIT: If need test subtrings:

df['Z'] = df.Y.str.contains('|'.join(words)).astype(int)
df['Z'] = df.Y.str.contains('|'.join(words)).view('i1')
df['Z'] = np.where(df.Y.str.contains('|'.join(words)),1,0)
Related