Pandas: counter increasing each time conditions are met
up vote
2
down vote
favorite
I have a dataframe with some conditions and a Counter that Counts when condition A is met.
date condition count
01,01,2018 08:00 A 1
01,01,2018 08:01 A 2
01,01,2018 08:03 A 3
01,01,2018 08:04 B 0
01,01,2018 08:07 B 0
01,01,2018 08:10 B 0
01,01,2018 08:13 B 0
01,01,2018 08:22 A 1
01,01,2018 08:24 A 2
01,01,2018 08:25 B 0
01,01,2018 08:27 B 0
01,01,2018 08:29 B 0
01,01,2018 08:30 A 1
I would like that the Count doesn't reset each time condition changes.
date condition count
01,01,2018 08:00 A 1
01,01,2018 08:01 A 2
01,01,2018 08:03 A 3
01,01,2018 08:04 B 3
01,01,2018 08:07 B 3
01,01,2018 08:10 B 3
01,01,2018 08:13 B 3
01,01,2018 08:22 A 4
01,01,2018 08:24 A 5
01,01,2018 08:25 B 5
01,01,2018 08:27 B 5
01,01,2018 08:29 B 5
01,01,2018 08:30 A 6
At the Moment the code for the Count Looks like:
df['count']= df.groupby((df['condition'] = 'A').cumsum()).cumcount()
Thanks!
python pandas
add a comment |
up vote
2
down vote
favorite
I have a dataframe with some conditions and a Counter that Counts when condition A is met.
date condition count
01,01,2018 08:00 A 1
01,01,2018 08:01 A 2
01,01,2018 08:03 A 3
01,01,2018 08:04 B 0
01,01,2018 08:07 B 0
01,01,2018 08:10 B 0
01,01,2018 08:13 B 0
01,01,2018 08:22 A 1
01,01,2018 08:24 A 2
01,01,2018 08:25 B 0
01,01,2018 08:27 B 0
01,01,2018 08:29 B 0
01,01,2018 08:30 A 1
I would like that the Count doesn't reset each time condition changes.
date condition count
01,01,2018 08:00 A 1
01,01,2018 08:01 A 2
01,01,2018 08:03 A 3
01,01,2018 08:04 B 3
01,01,2018 08:07 B 3
01,01,2018 08:10 B 3
01,01,2018 08:13 B 3
01,01,2018 08:22 A 4
01,01,2018 08:24 A 5
01,01,2018 08:25 B 5
01,01,2018 08:27 B 5
01,01,2018 08:29 B 5
01,01,2018 08:30 A 6
At the Moment the code for the Count Looks like:
df['count']= df.groupby((df['condition'] = 'A').cumsum()).cumcount()
Thanks!
python pandas
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a dataframe with some conditions and a Counter that Counts when condition A is met.
date condition count
01,01,2018 08:00 A 1
01,01,2018 08:01 A 2
01,01,2018 08:03 A 3
01,01,2018 08:04 B 0
01,01,2018 08:07 B 0
01,01,2018 08:10 B 0
01,01,2018 08:13 B 0
01,01,2018 08:22 A 1
01,01,2018 08:24 A 2
01,01,2018 08:25 B 0
01,01,2018 08:27 B 0
01,01,2018 08:29 B 0
01,01,2018 08:30 A 1
I would like that the Count doesn't reset each time condition changes.
date condition count
01,01,2018 08:00 A 1
01,01,2018 08:01 A 2
01,01,2018 08:03 A 3
01,01,2018 08:04 B 3
01,01,2018 08:07 B 3
01,01,2018 08:10 B 3
01,01,2018 08:13 B 3
01,01,2018 08:22 A 4
01,01,2018 08:24 A 5
01,01,2018 08:25 B 5
01,01,2018 08:27 B 5
01,01,2018 08:29 B 5
01,01,2018 08:30 A 6
At the Moment the code for the Count Looks like:
df['count']= df.groupby((df['condition'] = 'A').cumsum()).cumcount()
Thanks!
python pandas
I have a dataframe with some conditions and a Counter that Counts when condition A is met.
date condition count
01,01,2018 08:00 A 1
01,01,2018 08:01 A 2
01,01,2018 08:03 A 3
01,01,2018 08:04 B 0
01,01,2018 08:07 B 0
01,01,2018 08:10 B 0
01,01,2018 08:13 B 0
01,01,2018 08:22 A 1
01,01,2018 08:24 A 2
01,01,2018 08:25 B 0
01,01,2018 08:27 B 0
01,01,2018 08:29 B 0
01,01,2018 08:30 A 1
I would like that the Count doesn't reset each time condition changes.
date condition count
01,01,2018 08:00 A 1
01,01,2018 08:01 A 2
01,01,2018 08:03 A 3
01,01,2018 08:04 B 3
01,01,2018 08:07 B 3
01,01,2018 08:10 B 3
01,01,2018 08:13 B 3
01,01,2018 08:22 A 4
01,01,2018 08:24 A 5
01,01,2018 08:25 B 5
01,01,2018 08:27 B 5
01,01,2018 08:29 B 5
01,01,2018 08:30 A 6
At the Moment the code for the Count Looks like:
df['count']= df.groupby((df['condition'] = 'A').cumsum()).cumcount()
Thanks!
python pandas
python pandas
asked Nov 19 at 17:02
Luca91
1518
1518
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
Why not
df['count']=df['condition'].eq('A').cumsum()
what if I have 2 conditions but I want to restart the Count each time are both met?? at the Moment I am tryingdf['count']= df.groupby(((df.condition1<50)&(df.condition2 < 10)).cumsum()).cumcount()+1but it doesn't work, it simply start counting from the beginning to the end ignoring the conditions...can you help me with this please?
– Luca91
Nov 20 at 8:46
add a comment |
up vote
1
down vote
I think groupby.cumsum is what you're looking for
df['count']= df.groupby((df['Date']['condition']).cumsum())
and then later subset the df based on required condition.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Why not
df['count']=df['condition'].eq('A').cumsum()
what if I have 2 conditions but I want to restart the Count each time are both met?? at the Moment I am tryingdf['count']= df.groupby(((df.condition1<50)&(df.condition2 < 10)).cumsum()).cumcount()+1but it doesn't work, it simply start counting from the beginning to the end ignoring the conditions...can you help me with this please?
– Luca91
Nov 20 at 8:46
add a comment |
up vote
4
down vote
accepted
Why not
df['count']=df['condition'].eq('A').cumsum()
what if I have 2 conditions but I want to restart the Count each time are both met?? at the Moment I am tryingdf['count']= df.groupby(((df.condition1<50)&(df.condition2 < 10)).cumsum()).cumcount()+1but it doesn't work, it simply start counting from the beginning to the end ignoring the conditions...can you help me with this please?
– Luca91
Nov 20 at 8:46
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Why not
df['count']=df['condition'].eq('A').cumsum()
Why not
df['count']=df['condition'].eq('A').cumsum()
answered Nov 19 at 17:09
W-B
94.2k72857
94.2k72857
what if I have 2 conditions but I want to restart the Count each time are both met?? at the Moment I am tryingdf['count']= df.groupby(((df.condition1<50)&(df.condition2 < 10)).cumsum()).cumcount()+1but it doesn't work, it simply start counting from the beginning to the end ignoring the conditions...can you help me with this please?
– Luca91
Nov 20 at 8:46
add a comment |
what if I have 2 conditions but I want to restart the Count each time are both met?? at the Moment I am tryingdf['count']= df.groupby(((df.condition1<50)&(df.condition2 < 10)).cumsum()).cumcount()+1but it doesn't work, it simply start counting from the beginning to the end ignoring the conditions...can you help me with this please?
– Luca91
Nov 20 at 8:46
what if I have 2 conditions but I want to restart the Count each time are both met?? at the Moment I am trying
df['count']= df.groupby(((df.condition1<50)&(df.condition2 < 10)).cumsum()).cumcount()+1 but it doesn't work, it simply start counting from the beginning to the end ignoring the conditions...can you help me with this please?– Luca91
Nov 20 at 8:46
what if I have 2 conditions but I want to restart the Count each time are both met?? at the Moment I am trying
df['count']= df.groupby(((df.condition1<50)&(df.condition2 < 10)).cumsum()).cumcount()+1 but it doesn't work, it simply start counting from the beginning to the end ignoring the conditions...can you help me with this please?– Luca91
Nov 20 at 8:46
add a comment |
up vote
1
down vote
I think groupby.cumsum is what you're looking for
df['count']= df.groupby((df['Date']['condition']).cumsum())
and then later subset the df based on required condition.
add a comment |
up vote
1
down vote
I think groupby.cumsum is what you're looking for
df['count']= df.groupby((df['Date']['condition']).cumsum())
and then later subset the df based on required condition.
add a comment |
up vote
1
down vote
up vote
1
down vote
I think groupby.cumsum is what you're looking for
df['count']= df.groupby((df['Date']['condition']).cumsum())
and then later subset the df based on required condition.
I think groupby.cumsum is what you're looking for
df['count']= df.groupby((df['Date']['condition']).cumsum())
and then later subset the df based on required condition.
answered Nov 19 at 17:21
Ken Dekalb
14811
14811
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379461%2fpandas-counter-increasing-each-time-conditions-are-met%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown