Еще один способ использования numy put
import pandas as pd
import numpy as np
df = pd.DataFrame({'a': ['pika', 'halo', 'polo', 'boat', 'moan', 'nope'],
'b': ['dog', 'cat', 'dog', 'man', 'tan', 'dog']})
colors = ['black' , 'green', 'yellow']
df
a b
0 pika dog
1 halo cat
2 polo dog
3 boat man
4 moan tan
5 nope dog
-
# 'wrap' mode is not needed when replacement list is same
# size as the number of target values
np.put(df.b, np.where(df.b == 'dog')[0], colors, mode='wrap')
df
a b
0 pika black
1 halo cat
2 polo green
3 boat man
4 moan tan
5 nope yellow