Background:
I have the following code to make a dataframe from a list:
l = ['the cat meows',
'the dog barks',
'the bird chirps']
df = pd.DataFrame(l, columns=['Text'])
Output:
Text
0 the cat meows
1 the dog barks
2 the bird chirps
Desired Output:
Text Animal
0 the cat meows cat
1 the dog barks dog
2 the bird chirps bird
Approach:
I attempt to get the Desired Output using the following code:
#create list of animal names
animal_list = ['cat', 'dog', 'bird']
#extract names from 'Text' column using the names in 'animal_list'
#and create a new column containing extracted 'Text' names
df['Sound'] = df['Animal'].str.extract(r"(%s)"% animal_list)
Problem:
However, I get the following when I do so:
Text Animal
0 the cat meows t
1 the dog barks t
2 the bird chirps t
Question
How do I achieve my desired output?
animal_listor is the middle word everytime?animal_listare needed