Counting continues occurrence of a value in a column [duplicate]2019 Community Moderator ElectionPandas DataFrame: How to groupby consecutive valuesHow do I sort a dictionary by value?How can I count the occurrences of a list item?Drop data frame columns by nameHow do I replace NA values with zeros in an R dataframe?Renaming columns in pandasAdding new column to existing DataFrame in Python pandas“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasHow to count the NaN values in a column in pandas DataFrame

What was so special about The Piano that Ada was willing to do anything to have it?

What would be the most expensive material to an intergalactic society?

Does the US political system, in principle, allow for a no-party system?

The (Easy) Road to Code

Idiom for feeling after taking risk and someone else being rewarded

"If + would" conditional in present perfect tense

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

When to use a QR code on a business card?

What is this tube in a jet engine's air intake?

Help! My Character is too much for her story!

I can't die. Who am I?

Can one live in the U.S. and not use a credit card?

Trocar background-image com delay via jQuery

Is it possible to clone a polymorphic object without manually adding overridden clone method into each derived class in C++?

Giving a career talk in my old university, how prominently should I tell students my salary?

Writing text next to a table

ESPP--any reason not to go all in?

Is "cogitate" used appropriately in "I cogitate that success relies on hard work"?

Is there a logarithm base for which the logarithm becomes an identity function?

How to educate team mate to take screenshots for bugs with out unwanted stuff

Is this Paypal Github SDK reference really a dangerous site?

Why is there an extra space when I type "ls" on the Desktop?

Locked Away- What am I?

Either of .... (Plural/Singular)



Counting continues occurrence of a value in a column [duplicate]



2019 Community Moderator ElectionPandas DataFrame: How to groupby consecutive valuesHow do I sort a dictionary by value?How can I count the occurrences of a list item?Drop data frame columns by nameHow do I replace NA values with zeros in an R dataframe?Renaming columns in pandasAdding new column to existing DataFrame in Python pandas“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasHow to count the NaN values in a column in pandas DataFrame










1
















This question already has an answer here:



  • Pandas DataFrame: How to groupby consecutive values

    2 answers



I have a data frame like this:



index value
---- -----
1 A
2 A
3 A
4 A
5 B
6 B
7 A
8 B
9 C
10 C


I want to add a column to count continues occurrence of my value, like this:



index value continues-count
---- ----- ----------
1 A 1
2 A 2
3 A 3
4 A 4
5 B 1
6 B 2
7 A 1
8 B 1
9 C 1
10 C 2


I am able to do it using a loop but as my dataset is huge it takes forever!










share|improve this question













marked as duplicate by user3483203 pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 6 at 23:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    Can you please post your current solution?

    – blacksite
    Mar 6 at 22:26















1
















This question already has an answer here:



  • Pandas DataFrame: How to groupby consecutive values

    2 answers



I have a data frame like this:



index value
---- -----
1 A
2 A
3 A
4 A
5 B
6 B
7 A
8 B
9 C
10 C


I want to add a column to count continues occurrence of my value, like this:



index value continues-count
---- ----- ----------
1 A 1
2 A 2
3 A 3
4 A 4
5 B 1
6 B 2
7 A 1
8 B 1
9 C 1
10 C 2


I am able to do it using a loop but as my dataset is huge it takes forever!










share|improve this question













marked as duplicate by user3483203 pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 6 at 23:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    Can you please post your current solution?

    – blacksite
    Mar 6 at 22:26













1












1








1









This question already has an answer here:



  • Pandas DataFrame: How to groupby consecutive values

    2 answers



I have a data frame like this:



index value
---- -----
1 A
2 A
3 A
4 A
5 B
6 B
7 A
8 B
9 C
10 C


I want to add a column to count continues occurrence of my value, like this:



index value continues-count
---- ----- ----------
1 A 1
2 A 2
3 A 3
4 A 4
5 B 1
6 B 2
7 A 1
8 B 1
9 C 1
10 C 2


I am able to do it using a loop but as my dataset is huge it takes forever!










share|improve this question















This question already has an answer here:



  • Pandas DataFrame: How to groupby consecutive values

    2 answers



I have a data frame like this:



index value
---- -----
1 A
2 A
3 A
4 A
5 B
6 B
7 A
8 B
9 C
10 C


I want to add a column to count continues occurrence of my value, like this:



index value continues-count
---- ----- ----------
1 A 1
2 A 2
3 A 3
4 A 4
5 B 1
6 B 2
7 A 1
8 B 1
9 C 1
10 C 2


I am able to do it using a loop but as my dataset is huge it takes forever!





This question already has an answer here:



  • Pandas DataFrame: How to groupby consecutive values

    2 answers







python pandas dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 6 at 22:22









HamidHamid

12810




12810




marked as duplicate by user3483203 pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 6 at 23:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by user3483203 pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 6 at 23:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1





    Can you please post your current solution?

    – blacksite
    Mar 6 at 22:26












  • 1





    Can you please post your current solution?

    – blacksite
    Mar 6 at 22:26







1




1





Can you please post your current solution?

– blacksite
Mar 6 at 22:26





Can you please post your current solution?

– blacksite
Mar 6 at 22:26












1 Answer
1






active

oldest

votes


















3














It is just too boring to using shift and cumsum , let us try itertools



import itertools 
df['New']=list(itertools.chain(*[list(range(len(list(y))))for _,y in itertools.groupby(df.value)]))
df
Out[596]:
index value New
0 1 A 0
1 2 A 1
2 3 A 2
3 4 A 3
4 5 B 0
5 6 B 1
6 7 A 0
7 8 B 0
8 9 C 0
9 10 C 1



pandas way



df['New']=df.groupby((df.value!=df.value.shift()).ne(0).cumsum()).cumcount()+1





share|improve this answer

























  • Awesome! Thank you :)

    – Hamid
    Mar 6 at 22:43











  • @Hamid no worry , happy coding

    – Wen-Ben
    Mar 6 at 22:50

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














It is just too boring to using shift and cumsum , let us try itertools



import itertools 
df['New']=list(itertools.chain(*[list(range(len(list(y))))for _,y in itertools.groupby(df.value)]))
df
Out[596]:
index value New
0 1 A 0
1 2 A 1
2 3 A 2
3 4 A 3
4 5 B 0
5 6 B 1
6 7 A 0
7 8 B 0
8 9 C 0
9 10 C 1



pandas way



df['New']=df.groupby((df.value!=df.value.shift()).ne(0).cumsum()).cumcount()+1





share|improve this answer

























  • Awesome! Thank you :)

    – Hamid
    Mar 6 at 22:43











  • @Hamid no worry , happy coding

    – Wen-Ben
    Mar 6 at 22:50















3














It is just too boring to using shift and cumsum , let us try itertools



import itertools 
df['New']=list(itertools.chain(*[list(range(len(list(y))))for _,y in itertools.groupby(df.value)]))
df
Out[596]:
index value New
0 1 A 0
1 2 A 1
2 3 A 2
3 4 A 3
4 5 B 0
5 6 B 1
6 7 A 0
7 8 B 0
8 9 C 0
9 10 C 1



pandas way



df['New']=df.groupby((df.value!=df.value.shift()).ne(0).cumsum()).cumcount()+1





share|improve this answer

























  • Awesome! Thank you :)

    – Hamid
    Mar 6 at 22:43











  • @Hamid no worry , happy coding

    – Wen-Ben
    Mar 6 at 22:50













3












3








3







It is just too boring to using shift and cumsum , let us try itertools



import itertools 
df['New']=list(itertools.chain(*[list(range(len(list(y))))for _,y in itertools.groupby(df.value)]))
df
Out[596]:
index value New
0 1 A 0
1 2 A 1
2 3 A 2
3 4 A 3
4 5 B 0
5 6 B 1
6 7 A 0
7 8 B 0
8 9 C 0
9 10 C 1



pandas way



df['New']=df.groupby((df.value!=df.value.shift()).ne(0).cumsum()).cumcount()+1





share|improve this answer















It is just too boring to using shift and cumsum , let us try itertools



import itertools 
df['New']=list(itertools.chain(*[list(range(len(list(y))))for _,y in itertools.groupby(df.value)]))
df
Out[596]:
index value New
0 1 A 0
1 2 A 1
2 3 A 2
3 4 A 3
4 5 B 0
5 6 B 1
6 7 A 0
7 8 B 0
8 9 C 0
9 10 C 1



pandas way



df['New']=df.groupby((df.value!=df.value.shift()).ne(0).cumsum()).cumcount()+1






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 6 at 22:34


























community wiki





2 revs
Wen-Ben













  • Awesome! Thank you :)

    – Hamid
    Mar 6 at 22:43











  • @Hamid no worry , happy coding

    – Wen-Ben
    Mar 6 at 22:50

















  • Awesome! Thank you :)

    – Hamid
    Mar 6 at 22:43











  • @Hamid no worry , happy coding

    – Wen-Ben
    Mar 6 at 22:50
















Awesome! Thank you :)

– Hamid
Mar 6 at 22:43





Awesome! Thank you :)

– Hamid
Mar 6 at 22:43













@Hamid no worry , happy coding

– Wen-Ben
Mar 6 at 22:50





@Hamid no worry , happy coding

– Wen-Ben
Mar 6 at 22:50





Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived