Home > Blockchain >  Iterate over groups and output those groups which met .loc conditions
Iterate over groups and output those groups which met .loc conditions

Time:03-24

  1. I'm trying to iterate over groups ( groupedby AC No)
  2. The groups that meets the given condition (having 12 rows) as output. data.loc[(data['Position'] <= 3) & (data['Votes %'] > 10.0) ].shape[0]) == 12 are assigned a dummy output as 1.

Let's start fresh and simple I have stored my new filtered dataset as

q5_data = data.loc[(data['Position'] <= 3) & (data['Votes %'] > 10.0)]
print(q5_data.head(10))

Filtered dataset

   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   
2     2010-01-01         3  Dheerendra Pratap Singh  20886     14.5   
14    2010-01-01         1          Bhagirathi Devi  51993     41.5   
15    2010-01-01         2               Naresh Ram  22211     17.7   
16    2010-01-01         3             Subodh Kumar  20095     16.0   
31    2010-01-01         1     Satish Chandra Dubey  45022     38.1   
32    2010-01-01         2        Alok Prasad Verma  24794     21.0   
33    2010-01-01         3          Fakhruddin Khan  22381     18.9   
46    2010-01-01         1     Prabhat Ranjan Singh  67510     50.4   

                         Party        AC name  AC No  
0          Janata Dal (United)  Valmiki Nagar      1  
1         Rashtriya Janata Dal  Valmiki Nagar      1  
2          Bahujan Samaj Party  Valmiki Nagar      1  
14       Bharatiya Janta Party       Ramnagar      2  
15    Indian National Congress       Ramnagar      2  
16  Nationalist Congress Party       Ramnagar      2  
31       Bharatiya Janta Party   Narkatiaganj      3  
32    Indian National Congress   Narkatiaganj      3  
33                 Independent   Narkatiaganj      3  
46         Janata Dal (United)         Bagaha      4  

In above dataset I want to output only those groups or AC Name which have total count of 12.

CodePudding user response:

You can count matched values by mask by GroupBy.sum and then filter:

m = (data['Position'] <= 3) & (data['Votes %'] > 10.0) 
s = data.assign(test = m).groupby('AC No')['test'].sum()

out = s.index[s == 12].tolist()
  • Related