I’m having a hard time trying to figure this out, I have a data frame with multiple columns after merging 2.
JavaScript
x
4
1
ID name_x name_y age_x age_y
2
1 Steve Steve 40 40
3
2 John John 34 35
4
I have 2 list:
JavaScript
1
3
1
list_a = [name_x, age_X]
2
list_b = [name_y, age_y]
3
I need to compare each group of variables and if they are different add 1 to the key column
JavaScript
1
4
1
ID name_x name_y age_x age_y key
2
1 Steve Steve 40 40 0
3
2 John John 34 35 1
4
I was trying to use something like this:
JavaScript
1
4
1
for a in list_a:
2
for b in list_b:
3
master.loc[master[a] != master[b], 'key'] = +1
4
Advertisement
Answer
I would avoid loops. Is there any reason we cant do it directly? Lets try
JavaScript
1
8
1
df['key'] =(df[list_a].values!=df[list_b].values).sum(1)
2
3
4
5
ID name_x name_y age_x age_y key
6
0 1 Steve Steve 40 40 0
7
1 2 John John 34 35 1
8