pythonpandaslambda

replace non zero result with 1 using a lambda function python


I am calculating a binary result of whether or not values in columns have at least one match. It may chance that they have multiple matches. I want to return a result of 1 if they have >1 common value.

MRE below:

df=pd.DataFrame({'col1':['a|b|c','a|b|c','a|b|c'],'col2':['b|d|e|a','d|e|f','a']})
df['new']=df[['col1','col2']].apply(lambda x: len(set(x['col1'].split('|')).intersection(set(x['col2'].split('|')))),axis=1)

I want to change the return of any value >1 with 1, ie

pd.DataFrame({'col1':['a|b|c','a|b|c','a|b|c'],'col2':['b|d|e|a','d|e|f','a'],'new':[1,0,1]})

SOLVED

df['new']=df[['col1','col2']].apply(lambda x: 1 if len(set(x['col1'].split('|')).intersection(set(x['col2'].split('|')))) >=1 else 0,axis=1)

Solution

  • df['new']=df[['col1','col2']].apply(lambda x: 1 if len(set(x['col1'].split('|')).intersection(set(x['col2'].split('|')))) >=1 else 0,axis=1)