My pandas dataframe:
ID | String | Pet |
---|---|---|
1 | this is a cat | |
2 | hello dog | |
I would like to extract the pet from the 'String'
column and fill the 'Pet'
column accordingly.
The third row should be empty, and not filled by default.
My attempt:
df['Pet'] = np.where(df['String'].str.contains("cat"), "cat",
np.where(df['String'].str.contains("dog"), "dog", '0'))
Unfortunately the empty (third) row also gets filled in my attempt.
Thank you in advance for your help!