Skip to content
Advertisement

Find words in array and get their indexes in Dataframe in Pandas

I have a dataframe :

import pandas as pd
data = {'token_1': [['cat', 'run','today'],['dog', 'eat', 'meat']],
        'token_2': [[ 'in', 'the' , 'morning','cat', 'run', 'today',
                      'very', 'quick'],['dog', 'eat', 'meat', 'chicken', 'from', 'bowl']]}

df = pd.DataFrame(data)

I need to find words from column token_1 in token_2 and get their indixes in an array. Then get a list of indexes for each line, i expected this:

lst_indexes = [[3,4,5],
                [0,1,2]]

Advertisement

Answer

Use list comprehension with enumerate for indices:

L = [[i for i, x in enumerate(b) if x in a] for a, b in zip(df['token_1'], df['token_2'])]
print (L)
[[3, 4, 5], [0, 1, 2]]
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement