I am trying to label rows based on ID and year, If ID is appeared in the years [2017,2018,2019] and not appeared in [2020,2021,2022] then need to label it as 1, else 0. df1 = pd.DataFrame({'ID': ['AX1', 'AX1', 'AX1','AX1','AX1','AX1','AX2','AX2','AX2','AX3','AX3','AX4','AX4','AX4'],'year':[2017,2018,2019,2020,2021,2022,2019,2020,2022,2019,2020,2017,2018,2019]}) ID year 0 AX1 2017 1 AX1 2018 2 AX1 2019 3 AX1 2020 4 AX1 2021 5 AX1 2022 6 AX2 2019 7 AX2 2020 8 AX2 2022 9 AX3 2019 10 AX3 2020 11 AX4 2017 12 AX4 2018 13 AX4 2019 Expected output: ID year label 0 AX1 2017 0 1 Ax1 2018 0 2 AX1 2019 0 3 AX1 2020 0 4 AX1 2021 0 5 AX1 2022 0 6 AX2 2019 0 7 AX2 2020 0 8 AX2 2022 0 9 AX3 2019 0 10 AX3 2020 0 11 AX4 2017 1 12 AX4 2018 1 13 AX4 2019 1 In the above example ID: AX4 is tagged as 1, because it's the only ID that appears in the first set of years [2017,2018,2019] and not appear in the second set [2020,2021,2022]. How do I achieve this? Continue reading...