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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With