I have a pandas dataframe which is similar to the follow but a lot bigger and complicated.
import pandas as pd
d = {'weight': [70, 10, 65, 1], 'String1': ['Labrador is a dog',
'Abyssinian is a cat',
'German Shepard is a dog',
'pigeon is a bird']}
df = pd.DataFrame(data=d)
df
Output
| Weight | String | |
|---|---|---|
| 0 | 70 | Labrador is a dog |
| 1 | 10 | Abyssinian is a cat |
| 2 | 65 | German Shepard is a dog |
| 3 | 1 | pigeon is a bird |
I want to create a new column, 'animal' based on column 'string1'
search_list = ['dog','cat']
if in 'search_list', then populate the value from the search list, else populate 'other'
| Weight | String | animal | |
|---|---|---|---|
| 0 | 70 | Labrador is a dog | dog |
| 1 | 10 | Abyssinian is a cat | cat |
| 2 | 65 | German Shepard is a dog | dog |
| 3 | 1 | pigeon is a bird | other |
Please suggest how to do this. Thank you.