How can I vectorize this loop?

Viewed 36

How can this loop be vectorized? Or can it? I just don't see it.

for c in cats:
    df[c] = np.where(df['categories'].str.contains(c),1,0)   

(Before reading the full story below, expect that you will be very tempted to answer what appears to be the a one-hot encoding question with get_dummies(sep=';). But that is not really what I am asking. What I strictly want answered is the question above - about vectorizing that loop. I included the full story for context only.)

The full story:

d = {'categories': ['catA;catB;catC','catA;catB','catC','catB;catC']}
df = pd.DataFrame(d)

print(df)

       categories
0  catA;catB;catC
1       catA;catB
2            catC
3       catB;catC

cats = ['catA','catB','catC']

for c in cats:
    df[c] = np.where(df['categories'].str.contains(c),1,0)
    
print(df)

       categories  catA  catB  catC
0  catA;catB;catC     1     1     1
1       catA;catB     1     1     0
2            catC     0     0     1
3       catB;catC     0     1     1

(This is extraneous text added to the bottom of this question so that SO will allow my question to be submitted - to satisfy the requirement for more text in the question. I certainly agree with that requirement but in this case I think the above text and code adequately describes the situation.)

1 Answers

Try with get_dummies

df.categories.str.get_dummies(sep=';')
Out[113]: 
   catA  catB  catC
0     1     1     1
1     1     1     0
2     0     0     1
3     0     1     1
df = df.join(df.categories.str.get_dummies(sep=';'))
Related