您可以使用find
從np.core.defchararray
np.core.defchararray.find(frame.b.values.astype(str), frame.a)
array([3, 0, 7])
總結本了assign
frame.assign(c=np.core.defchararray.find(frame.b.values.astype(str), frame.a))
a b c
0 L }JKLMNOPQR 3
1 } }JKLMNOPQR 0
2 P }JKLMNOPQR 7
定時
很難被擊敗@ Psidom的zip
下面的代碼
pir1 = lambda d: d.assign(c=np.core.defchararray.find(d.b.values.astype(str), d.a.values.astype(str)))
pir2 = lambda d: d.assign(c=(np.array([list(x) for x in d.b.values.tolist()]) == d.a.values[:, None]).argmax(1))
psi = lambda d: d.assign(c=[b.find(a) for a, b in zip(d.a, d.b)])
jso = lambda d: d.assign(c=d.apply(lambda row: row.b.find(row.a), axis=1))
results = pd.DataFrame(
index=pd.Index([10, 30, 100, 300, 1000, 3000, 10000, 30000]),
columns='pir1 pir2 psi jso'.split()
)
for i in results.index:
d = pd.concat([frame] * i, ignore_index=True)
for j in results.columns:
stmt = '{}(d)'.format(j)
setp = 'from __main__ import d, {}'.format(j)
results.set_value(i, j, timeit(stmt, setp, number=10))
results.plot(loglog=True)