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
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!
python pandas dataframe
marked as duplicate by user3483203
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.
add a comment |
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!
python pandas dataframe
marked as duplicate by user3483203
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
add a comment |
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!
python pandas dataframe
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
python pandas dataframe
asked Mar 6 at 22:22
HamidHamid
12810
12810
marked as duplicate by user3483203
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
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
Awesome! Thank you :)
– Hamid
Mar 6 at 22:43
@Hamid no worry , happy coding
– Wen-Ben
Mar 6 at 22:50
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Awesome! Thank you :)
– Hamid
Mar 6 at 22:43
@Hamid no worry , happy coding
– Wen-Ben
Mar 6 at 22:50
add a comment |
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
Awesome! Thank you :)
– Hamid
Mar 6 at 22:43
@Hamid no worry , happy coding
– Wen-Ben
Mar 6 at 22:50
add a comment |
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
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
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
add a comment |
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
add a comment |
1
Can you please post your current solution?
– blacksite
Mar 6 at 22:26