I have a pandas data frame of 30 000 rows that looks like this:
ID year month var1-var300 test
1111 2017 7 ... 1
1111 2017 9 ... 0
2222 2017 6 ... 1
2222 2017 6 ... 0
2222 2016 6 ... 0
3333 2017 3 ... 1
3333 2017 3 ... 0
3333 2015 8 ... 0
...
Here is what I want to do for each row: if test=1, I would like to extract the variables "ID year month", loop over the entire data frame and if this combination of variables is found in any other row, assign 1 to a new variable 'check'. The final data frame should look like this:
ID year month var1-var300 test check
1111 2017 7 ... 1 0
1111 2017 9 ... 0 0
2222 2017 6 ... 1 1
2222 2017 6 ... 0 0
2222 2016 6 ... 0 0
3333 2017 3 ... 1 1
3333 2017 3 ... 0 0
3333 2015 8 ... 0 0
...
Here is some kind of pseudo-code I have imagined:
for line in df:
if line['test']=1:
I=line['ID']
Y=line['year']
MO=line['month']
for row in df:
if row['ID']=I & row['year']=Y & row['month']=MO:
line['check']=1
break
Any idea how to do a similar code that works in Python?