Questions tagged [running-count]
60 questions
                    
                    242
                    
            votes
                
                11 answers
            
        Numbering rows within groups in a data frame
Working with a data frame similar to this:
set.seed(100)  
df <- data.frame(cat = c(rep("aaa", 5), rep("bbb", 5), rep("ccc", 5)), val = runif(15))             
df <- df[order(df$cat, df$val), ]  
df  
   cat        val  
1  aaa 0.05638315  
2  aaa…
         
    
    
        eli-k
        
- 10,898
- 11
- 40
- 44
                    78
                    
            votes
                
                5 answers
            
        Add a sequential counter column on groups to a pandas dataframe
I feel like there is a better way than this:
import pandas as pd
df = pd.DataFrame(
    columns="   index    c1    c2    v1 ".split(),
    data= [
            [       0,  "A",  "X",    3, ],
            [       1,  "A",  "X",    5, ],
            [ …
         
    
    
        Owen
        
- 3,063
- 5
- 30
- 26
                    33
                    
            votes
                
                6 answers
            
        Cumulative count of each value
I want to create a cumulative counter of the number of times each value appears.
e.g. say I have the column:
id  
1  
2  
3  
2  
2  
1  
2  
3
This would become:
id   count  
1     1  
2     1  
3     1  
2     2  
2     3  
1     2  
2     4  
3 …
         
    
    
        user1165199
        
- 6,351
- 13
- 44
- 60
                    27
                    
            votes
                
                5 answers
            
        Add a sequence number to each element in a group using python
I have a dataframe of individuals who each have multiple records.  I want to enumerate the record in the sequence for each individual in python.  Essentially I would like to create the 'sequence' column in the following table:
patient  date     …
         
    
    
        DKA
        
- 1,087
- 1
- 9
- 11
                    23
                    
            votes
                
                3 answers
            
        Enumerate each row for each group in a DataFrame
In pandas, how can I add a new column which enumerates rows based on a given grouping?
For instance, assume the following DataFrame:
import pandas as pd
import numpy as np
a_list = ['A', 'B', 'C', 'A', 'A', 'C', 'B', 'B', 'A', 'C']
df =…
         
    
    
        Greg Reda
        
- 1,744
- 2
- 13
- 20
                    14
                    
            votes
                
                4 answers
            
        Pandas cumulative count
I have a data frame like this:
0        04:10  obj1
1        04:10  obj1
2        04:11  obj1
3        04:12  obj2
4        04:12  obj2
5        04:12  obj1
6        04:13  obj2
Wanted to get a cumulative count for all the objects like this:
idx   …
         
    
    
        jincept
        
- 279
- 2
- 5
- 16
                    13
                    
            votes
                
                4 answers
            
        Creating a running counting variable in R?
I have a dataset of soccer match results, and I am hoping to learn R by creating a running set of ratings similar to the World Football Elo formula.  I am running into trouble with things that seem to be simple in Excel aren't exactly intuitive in…
         
    
    
        Matt Barger
        
- 173
- 1
- 6
                    11
                    
            votes
                
                4 answers
            
        SQL Cumulative Count
I have table with departments. I need to count how many people are within which dept. This is easily done by
SELECT DEPT,
       COUNT(*) as 'Total'
    FROM SR
    GROUP BY DEPT;
Now I need to also do cumulative count as below:
I have found some…
         
    
    
        DNac
        
- 2,663
- 8
- 31
- 54
                    10
                    
            votes
                
                2 answers
            
        Number duplicates sequentially in Pandas DataFrame
I have a Pandas DataFrame that has a column that is basically a foreign key, as below:
Index
f_key
values
0
1
red
1
2
blue
2
1
green
3
2
yellow
4
3
orange
5
1
violet
What I would like is to add a column that  labels the…
         
    
    
        Rick Berg
        
- 148
- 2
- 10
                    8
                    
            votes
                
                3 answers
            
        R cumulative sum by condition with reset
I have a vector of numbers in a data.frame such as below.
df <- data.frame(a = c(1,2,3,4,2,3,4,5,8,9,10,1,2,1))
I need to create a new column which gives a running count of entries that are greater than their predecessor. The resulting column…
         
    
    
        nsymms
        
- 115
- 1
- 1
- 7
                    8
                    
            votes
                
                3 answers
            
        Running count based on field in R
I have a data set of this format
User       
1 
2
3
2
3
1  
1      
Now I want to add a column saying count which counts the occurrence of the user. I want output in the below format.
User    Count
1       1
2       1 
3       1
2       2
3      …
         
    
    
        Gughan
        
- 133
- 3
- 8
                    8
                    
            votes
                
                1 answer
            
        How to create subindex efficiently?
I would like to create a subindex for my dataframe based on the index. For example, I have a dataframe like this:
      Content        Date
ID                       
Bob  birthday  2010.03.01
Bob    school  2010.04.01
Tom  shopping  2010.02.01
Tom  …
         
    
    
        Zhen Sun
        
- 817
- 3
- 13
- 20
                    8
                    
            votes
                
                1 answer
            
        Calculating a running count & running total across customers with SQL
I have the following table (SQL Server 2012):
DID - cust id
GID - order id
AMT - order amt
Gf_Date - order date
SC - order reversal amount
I'm trying to calculate a running count of orders and a running total of sales by customer so that I can…
         
    
    
        Chris Howley
        
- 81
- 1
- 1
- 4
                    7
                    
            votes
                
                2 answers
            
        creating a column which keeps a running count of consecutive values
I am trying to create a column (“consec”) which will keep a running count of consecutive values in another (“binary”) without using loop. This is what the desired outcome would look like:
.    binary consec
1       0      0
2       1      1
3      …
         
    
    
        MJS
        
- 1,573
- 3
- 17
- 26
                    5
                    
            votes
                
                1 answer
            
        How to add sequence number for each element in a group using a SQL query without temp tables
My question is quite similar to the one posted in this link - How to add sequence number for groups in a SQL query without temp tables
But, I need to enumerate the occurrence of group. The final output to be like…
         
    
    
        user1346265
        
- 53
- 1
- 1
- 3