!! The aim is to apply the working method to text in a Pandas Dataframe !!
Given that I have sentences like the following ones:
"He invited 2 people and pet 3 dogs."
"She invited 3 friends and pet 1 cat."
For each sentence I want to count in a variable how many humans are invited and how many pets are pet. This works easily via regex:
sentence = 'He invited 2 people and pet 3 dogs.' human = [r'(\d+) people', r'(\d+) friend'] for h in human: number = re.search(h, sentence, re.IGNORECASE) if number is not None: number = number.group(1) print('humans invited: ',number)
Now the sentences are in a Pandas Dataframe in the column "sentence". The Dataframe also has a column called "humans" and one called "pets". I now want to take the first sentence, process it like shown above, write the result for humans into the column "humans", do the same for pets and write it into the column pets. However, I am not sure how to apply this to a Pandas Dataframe row by row.