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
1618
1618
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()+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 |
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()+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
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()+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
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
96.7k73162
96.7k73162
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()+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 |
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()+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
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
15911
15911
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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