Questions tagged [running-count]
60 questions
4
votes
1 answer
Count duplicate rows and fill in column
I have created a DataFrame, and now need to count each duplicate row (by for example df['Gender']. Suppose Gender 'Male' occurs twice and Female three times, I need this column to be made:
Gender Occurrence
Male 1
Male 2
Female 1
Female …

J. Williams
- 135
- 3
- 10
4
votes
2 answers
Identify Duplicated rows with Additional Column
I have the following Dataframe:
PplNum RoomNum Value
0 1 0 265
1 1 12 170
2 2 0 297
3 2 12 85
4 2 0 41
5 2 12 144
Generally the PplNum and RoomNum…
user6393964
3
votes
2 answers
How to calculate number of days until weekend or day off in pandas dataframe
I have pandas dataframe with a non-continuous date index (missing are weekends and holidays). I want to add column which would contain number of days until next day off.
Here is code generating example dataframe with desired values in till_day_off…

Josef Švenda
- 31
- 3
3
votes
1 answer
Running count based on multiple columns
I would like to create a running count of the values in the "Button" column (Start, Stop) within each "ID". However, any change in the "Button" value or a change of "ID" should reset the running count. The data frame is below:
data =…

Dfeld
- 187
- 9
3
votes
2 answers
How to add one column according to the order after `groupby` in pandas?
My data is like and I want to calculate the NDCG metric
pid query title label predict prob
0 1 A a 0 2 0.9
1 1 A b 2 0 0.8
2 1 A c 1 2 0.7
3 2 D d 3 1…

danche
- 1,775
- 15
- 22
3
votes
2 answers
Iterating through pandas groupby groups
I have a pandas dataframe school_df that looks like this:
school_id date_posted date_completed
0 A 2014-01-01 2014-01-01
1 A 2014-01-01 2014-01-08
2 A 2014-04-29 2014-05-01
3 B 2014-01-01 …

Erin
- 33
- 2
2
votes
2 answers
SQL query to obtain users seen from a particular variabe date
I have a table "days_users" with dates and users (one row per date and user), from where I could extract the information of which particular days a user was seen, or which users were seen on a particular…

ElTitoFranki
- 375
- 1
- 7
2
votes
2 answers
Pandas: number rows within group cumulatively and across another group
Given the following dataframe:
col_1 col_2 col_3
0 1 A 1
1 1 B 1
2 2 A 3
3 2 A 3
4 2 A 3
5 2 B 3
6 2 B 3
7 2 B 3
8 3 A 2
9 3 A …

cacadudi
- 23
- 3
2
votes
1 answer
Conditional cumcount of values in second column
I want to fill numbers in column flag, based on the value in column KEY.
Instead of using cumcount() to fill incremental numbers, I want to fill same number for every two rows if the value in column KEY stays same.
If the value in column KEY…

franky
- 45
- 4
2
votes
1 answer
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:
user …

onurkorkmaz
- 45
- 4
2
votes
2 answers
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.…

user3116949
- 265
- 1
- 5
- 14
2
votes
1 answer
find equal time and incrementally add a constant
I have a dataframe df containing some timestamps
df['Date'].values
Out[16]:
array(['2015-03-25T14:36:39.199994000', '2015-03-25T14:36:39.199994000',
'2015-03-26T10:05:03.699999000', '2015-04-19T16:01:49.680009000',
…

gabboshow
- 5,359
- 12
- 48
- 98
2
votes
1 answer
Add group indexers to pandas dataframe
I have some data that looks something like this:
date_time, user, page
12345, A, index
13456, A, index
14566, B, home
...
I'd like to store the index of each row (i.e., its order when sorted by date_time), both overall, and per page.
Overall is…

Jeremy
- 1,960
- 4
- 21
- 42
1
vote
1 answer
Pandas cumulative count across different groups
I've got the following DataFrame :
df = pd.DataFrame({'A': ['Nadal', 'Federer', 'Djokovic', 'Nadal', 'Nadal', 'Murray', 'Nadal'],
'B': ['Djokovic', 'Nadal', 'Murray', 'Murray', 'Djokovic', 'Federer', 'Murray'],
…

Wazzabeee
- 35
- 5
1
vote
2 answers
Python Pandas DataFrame: create a counter in the column based on start-counting condition
I want to program a counter with python which counts the "1-values" in the column "values". The counter has to start counting after the first 0 value in the rows.
As it is seen in the example:
the counter value for the first three "1-values" is…

jess
- 81
- 1
- 7