Search code examples
pythonarrayspandasindexingfind

Pandas : Find which index in array is equals to last element of other array


I have two columns with arrays which are generated from two others

mf['FV_array'] = mf['FV'].str.split()
mf['MT_array'] = mf['MT'].str.split()

and now what i am tryin to do is find which mf['MT_array'] index in every row is equals to last index in every mf['FV_array'], a lot of my probes with index function did not work.

Example what i would like to do :

FV_array                                |   MT_array     |  Result

['Something','John','Doe','is','there']   ['John','Doe']      2

Thank you in advance for help!


Solution

  • You can use apply() to call index() on the elements in each row.

    mf['Result'] = mf.apply(lambda row: row['FV_array'].index(row['MT_array'][-1]) if row['MT_array'][-1] in row['FV_array'] else None, axis=1)