I have this dataframe: I want to add a new column Sequence with a sequence of numbers. The condition is when the first True appears in the Condition column, the following rows must contain the sequence 1, 2, 3, 1, 2, 3… until another True appears again, at which point the sequence is restarted again. Furthermore, ideally, until the first
Tag: running-count
Pandas Cumcount() over multiple columns
I have a dataframe that looks like this: What I want to do is to add two columns, one for set number and one for rep number. Set number should increase by 1 for each weight change exercise & session is the same, else reset to 0. Rep number should increase by 1 for each velocity change if exercise, session
How can I add a counter column that counts specific values in another column in a pandas dataframe?
I have a large dataframe (>16M rows) which has a column named ‘user’. Every user have more than one occurrences. I want to add a new column ‘counter’ that increases every time a specific user has a new record. The dataframe looks like this: I want it to look like this with the new counter column I tried the following
How to create rank column in Python based on other columns
I have a python dataframe that looks like the following: This dataframe has been sorted in descending order by ‘transaction_count’. I want to create another column in that dataframe called ‘rank’ that contains the count of occurrences of cust_ID. My desired output would look something like the following: For cust_ID = 1234 with transaction_count = 4, the rank would be
Iterating through pandas groupby groups
I have a pandas dataframe school_df that looks like this: Each row represents one project by that school. I’d like to add two columns: for each unique school_id, a count of how many projects were posted before that date and a count of how many projects were completed before that date. The code below works, but I have ~300,000 unique
Add a sequential counter column on groups to a pandas dataframe
I feel like there is a better way than this: To achieve this: Is there a way to do it that avoids the callback? Answer use cumcount(), see docs here If you want orderings starting at 1