Mohamed AL ANI
Mohamed AL ANI

Reputation: 2072

find position of column string in another column using Pandas

I have a Dataframe with 2 columns

   col1     col2
1  cat      the cat
2  dog      a nice dog
3  horse    horse is here

I need to find the position of each string of col1 in col2.

Solution must be:

   col1     col2          col3
1  cat      the cat        4
2  dog      a nice dog     7
3  horse    horse is here  0

There must be a simple solution to do this without using painful loops, but i can't find it.

Upvotes: 5

Views: 3088

Answers (2)

piRSquared
piRSquared

Reputation: 294488

numpy.core.defchararray.find

from numpy.core.defchararray import find

a = df.col2.values.astype(str)
b = df.col1.values.astype(str)
df.assign(col3=find(a, b))

    col1           col2  col3
1    cat        the cat     4
2    dog     a nice dog     7
3  horse  horse is here     0

Upvotes: 8

sacuL
sacuL

Reputation: 51395

In pandas when working with strings, often loops or list comprehensions will be faster than the built-in string methods. In your case, it can be a pretty short one:

df['col3'] = [i2.index(i1) for i1,i2 in zip(df.col1,df.col2)]

>>> df
    col1           col2  col3
1    cat        the cat     4
2    dog     a nice dog     7
3  horse  horse is here     0

Upvotes: 5

Related Questions