Search code examples
pythonpandaslistcumsum

Store the index values that add to a specific cumsum


I have the following df

County      TotPerson  
Wayne       148        
Oakland     125        
Macomb      63         
Washtenaw   30          
Ingham      30          
Monroe      28          
Hillsdale   15          
Livingstone 15          
Jackson     14          
Lenawee     12        

I'd like to store in different lists or a dictionary (it really doesn't matter) the counties that have a cumsum of no more than 190 from top to bottom.

The result should look something like this:

Group1
[Wayne]

Group2
[Oakland,Macomb]

Group3
[Washtenaw, Ingham, Monroe, Hillsdale, Livingstone, Jackson, Lenawee]  

Solution

  • groups = []
    
    for i in range(len(df)):
        if len(df)>0:
            groups.append(df.loc[df.TotPerson.cumsum().lt(190)].County.tolist())
            df = df.loc[df.TotPerson.cumsum().ge(190)]
    
    [['Wayne'],
     ['Oakland', 'Macomb'],
     ['Washtenaw',
      'Ingham',
      'Monroe',
      'Hillsdale',
      'Livingstone',
      'Jackson',
      'Lenawee']]