1

I have a data frame:

df=pd.DataFrame({'col1':['a','c','d','f'],'col2':[1,2,3,4]})

and want to create a new column that is 1/0 if the value in col1 is an element in a list, lst=['a','b','c']

I am using:

df['in_lst']=df.col1.apply(lambda x: int(x in lst))

to get

    col1    col2    in_lst
0   a       1       1
1   c       2       1
2   d       3       0
3   f       4       0

my actual dataframe in 100k rows, and my list length is 1k.

This is extremely slow at the moment, is there a way I can speed this up?

Any suggestions

frank
  • 3,036
  • 7
  • 33
  • 65

0 Answers0