python - Vectorized way of finding position of one column value in another in Pandas -
basically following code returns position of character in column 'a' in string (in case '}jklmnopqr'). in example, column 'b' has same value in rows, have different values.
is there vectorized way this?
frame = pd.dataframe({'a' : ['l', '}', 'p']}) frame['b']='}jklmnopqr' frame['c'] = frame.apply(lambda row: row.b.find(row.a), axis=1) frame b c 0 l }jklmnopqr 3 1 } }jklmnopqr 0 2 p }jklmnopqr 7
not vectorized faster solution using zip
:
lframe1 = pd.concat([frame]*1000) lframe2 = pd.concat([frame]*1000) %timeit lframe1['c'] = lframe1.apply(lambda row: row.b.find(row.a), axis=1) # 10 loops, best of 3: 77.7 ms per loop %timeit lframe2['c'] = [b.find(a) a, b in zip(lframe2.a, lframe2.b)] # 1000 loops, best of 3: 1.4 ms per loop lframe1.c.eq(lframe2.c).all() # true
Comments
Post a Comment