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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page