Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new column based on condition applied from two other string columns in python

I have data in the below format :

pastLocation | currentLocation    
delhi        | bangalore          
delhi        | london,pune,delhi  
mumbai       | mumbai             
pune         | pune, noida       

I have to create a new column named as changeInLocation where if pastLocation is present in currentLocation then value of new column would be 0 else 1. For example, in second row, pastLocation i.e. Delhi is present in corresponding currentLocation so value of changeInLocation should be 0

Output should be in following format:

pastLocation | currentLocation   | changeInLocation
delhi        | bangalore         | 1
delhi        | london,pune,delhi | 0
mumbai       | mumbai            | 0
pune         | pune, noida       | 0
like image 776
Mighty Avatar asked Jan 28 '23 03:01

Mighty


1 Answers

Use apply with in for check membership and then cast to int:

df['changeInLocation'] = df.apply(lambda x: x['pastLocation'] not in x['currentLocation'], axis=1).astype(int)

Another solution iz zip columns and use list comprehension:

df['changeInLocation'] = [int(a not in b) for a, b in zip(df['pastLocation'], df['currentLocation'])]

print (df)
  pastLocation    currentLocation  changeInLocation
0        delhi          bangalore                 1
1        delhi  london,pune,delhi                 0
2       mumbai             mumbai                 0
3         pune        pune, noida                 0
like image 92
jezrael Avatar answered Jan 31 '23 10:01

jezrael