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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With