I have a dataframe:
import pandas as pd
data = {'token_1': [['cat', 'bag', 'sitting'],
['dog', 'eats', 'bowls'],
['mouse', 'mustache', 'tail'],
['dog', 'eat', 'meat']],
'token_2': [['cat', 'from', 'bag', 'cat', 'in', 'bag', 'sitting', 'whole', 'day'],
['dog', 'eats', 'from', 'bowls', 'dog', 'eats', 'always', 'from', 'bowls', 'eats', 'bowl'],
['mouse', 'with', 'a', 'big', 'tail', 'and,' 'ears', 'a', 'mouse', 'with', 'a', 'mustache', 'and', 'a', 'tail' ,'runs', 'fast'],
['dog', 'eat', 'meat', 'chicken', 'from', 'bowl','dog','see','meat','eat']]}
df = pd.DataFrame(data)
I have no conjunctions and prepositions in the token_1 column. I would like to find them from the token_2 column.That is, find the words between the crossed tokens.
As far as I understand there are several steps:
- check the first intersection of
token_1[0] - look if the next word is less than the length of 4, if yes then add it in list. If not, then go to the first intersection of
token_1[1] - Again we look if the next word is less than length 4
- Repeat the process until we reach the last
token_1[2] - If there is nothing between the tokens, then return them
Or is there some simpler method?Ultimately, I would like to get a new_token column:
+-----------------------+---------------------------------+--------------------------------------------------------------------------------------------+
|token_1 |new_tokens |token_2 |
+-----------------------+---------------------------------+--------------------------------------------------------------------------------------------+
|[cat, bag, sitting] |[cat, in, bag, sitting] |[cat, from, bag, cat, in, bag, sitting, whole, day] |
|[dog, eats, bowls] |[dog, eats, from, bowls] |[dog, eats, from, bowls, dog, eats, always, from, bowls, eats, bowl] |
|[mouse, mustache, tail]|[mouse, with,mustache, and, tail]|[mouse, with, a, big, tail, and,ears, a, mouse, with, a, mustache, and, a, tail, runs, fast]|
|[dog, eat, meat] |[dog, eat, meat] |[dog, eat, meat, chicken, from, bowl, dog, see, meat, eat] |
+-----------------------+---------------------------------+--------------------------------------------------------------------------------------------+
Thanks for the help!