'Difference between adjacent row values for each Group

I'm trying to find those AC No. whose difference between Votes % is less than 10.0 % in every election. This is me dataframe :

   Election Year  Position                   Name  Votes  Votes %  \
0     2010-01-01         1           Rajesh Singh  42289     29.4   
1     2010-01-01         2  Mukesh Kumar Kushwaha  27618     19.2   
14    2010-01-01         1        Bhagirathi Devi  51993     41.5   
15    2010-01-01         2             Naresh Ram  22211     17.7   
31    2010-01-01         1   Satish Chandra Dubey  45022     38.1   
32    2010-01-01         2      Alok Prasad Verma  24794     21.0   
46    2010-01-01         1   Prabhat Ranjan Singh  67510     50.4   
47    2010-01-01         2       Ram Prasad Yadav  18455     13.8   
61    2010-01-01         1           Vinay Bihari  38381     33.4   
62    2010-01-01         2          Pradeep Singh  27500     24.0   

                       Party        AC name  AC No  
0        Janata Dal (United)  Valmiki Nagar      1  
1       Rashtriya Janata Dal  Valmiki Nagar      1  
14     Bharatiya Janta Party       Ramnagar      2  
15  Indian National Congress       Ramnagar      2  
31     Bharatiya Janta Party   Narkatiaganj      3  
32  Indian National Congress   Narkatiaganj      3  
46       Janata Dal (United)         Bagaha      4  
47      Rashtriya Janata Dal         Bagaha      4  
61               Independent        Lauriya      5  
62       Janata Dal (United)        Lauriya      5  

I have tried this code:

def f(x):
    if (abs(q7_data['Votes %'] - q7_data['Votes %'].shift(-1))< 10):
        return True
    else:
        return False
#   
q7_data['Close Contest'] = q7_data.groupby(['Election Year','AC No'])['Votes %'].transform(f)

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

What Close Contest column outputs

  1. First I need to groupby Election Year and AC No.
  2. Within that group take difference between consecutive rows(only two rows per group).
  3. Return True or False.

I also wanted to do check if the 'Close contest' is True for all elections for a given AC no. but unable to integrate it into one line.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source