title: Comparing the rows of a column in python
body: <p>I have the below data frame </p>
  
  <hr>
  
  <pre><code>df=
   city    code     qty    year
   hyd     1        10    2016
   hyd     2        12    2016
   pune    2        15    2016
   pune    4        25    2016
   hyd     1        10    2017
   hyd     3        12    2017
   pune    1        15    2017
   pune    2        25    2017
   hyd     2        10    2018
   hyd     4        10    2018
   hyd     6        12    2018
   pune    1        15    2018
   pune    4        25    2018
  </code></pre>
  
  <p>I want to add all the unique years as columns here(2016,2017,2018) and compare if same city and code of one year with other years less than the year compared to(i.e. for 2018 compare with 2017,2016,2015 and for 2017 with 2016,2015 and so on..). if same city and code is available in other years then flag it as Y if not present then N. And the the with which we are comparing must be left blank. </p>
  
  <p>The below must be the result data frame.</p>
  
  <hr>
  
  <pre><code>city    code     qty    year    year_2016     year_2017    year_2018 
  hyd     1         10    2016                                 
  hyd     2         12    2016                                         
  pune    2         15    2016                                  
  pune    4         25    2016                                
  hyd     1         10    2017        Y                                          
  hyd     3         12    2017        N                         
  pune    1         15    2017        N                           
  pune    2         25    2017        Y                         
  hyd     2         10    2018        Y            N        
  hyd     4         12    2018        N            N
  hyd     6         12    2018        N            N
  pune    1         15    2018        N            Y
  pune    4         25    2018        Y            N     
  </code></pre>
  
  <p>Thanks in advance</p>