Replace the dataframe entries with binary value

Question:

Trying to replace certain strings with a binary value. I tried to find and replace but it only works for one value. I’d like to do it for multiple different labels.
I’d like to replace dog and bat with 1 and cat and snail with 0 :

animal 
0 cat 
1 dog
2 snail
3 bat
4 deer

To:

  animal 
0 0
1 1
2 0
3 1
4 0

Here is my sample code:

cold=['cat','snail','deer']
df['animal'] = np.where(df['animal']=cold, '0', '1')
Asked By: user18774110

||

Answers:

Your code almost works, just need a slight change.

df['animal'] = np.where(df['animal'].isin(cold), '0', '1')

  animal
0      0
1      1
2      0
3      1
4      0

Or you could use the answer in the comment.

df['animal'] = (~df['animal'].isin(cold)).astype(int)

I Like np.select the most:

import numpy as np

cold = ['cat','snail','deer']


df = (df
 .assign(animal=lambda x: np.select([x.animal.isin(cold)],
                                    [0], default=1)
        )
)

Answered By: William Rosenbaum
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.