Pandas ColumnA in ColumnB (not .apply)

14 views
Skip to first unread message

Peter Mc

unread,
Nov 28, 2016, 11:09:57 PM11/28/16
to PyData
import pandas as pd
import numpy as np

d = {'one' : [1., 2., 3., 4.],'two' : [4., 3., 2., 1.],
     'three':['abbaa','bbcfb','ccdecc','ddfgddd'],
     'four':['abba','bcdf','de','fg']}
df = pd.DataFrame(d)
df

>>
fouronethreetwo
0abba1.0abbaa4.0
1bcdf2.0bbcfb3.0
2de3.0ccdecc2.0
3fg4.0ddfgddd1.0

df.apply(lambda x: x['four'] in x['three']   , axis=1)

>>
0     True
1    False
2     True
3     True
dtype: bool

Is there anyway to do this column wise, and not row wise, on a large dataset this takes exceptionally long,

?df['three'].str.contains(df['four'].str)

Thanks
Reply all
Reply to author
Forward
0 new messages