How do I count how often a column value changes in a pandas dataframe?

stickersiskool

I have a pandas data frame that looks like:

Index  Activity
0          0
1          0
2          1
3          1
4          1
5          0
...
1167       1
1168       0
1169       0

I want to count how many times it changes from 0 to 1 and when it changes from 1 to 0, but I do not want to count how many 1's or 0's there are. For example, if I only wanted to count index 0 to 5, the count for 0 to 1 would be one.

How would I go about this? I have tried using some_value

Tanoy Majumdar

This is a simple approach that can also tell you the index value when the change happens. Just add the index to a list.

c_1to0 = 0
c_0to1 = 0
for i in range(0, df.shape[0]-1):
    if df.iloc[i]['Activity'] == 0 and df.iloc[i+1]['Activity'] == 1:
        c_0to1 +=1
    elif df.iloc[i]['Activity'] == 1 and df.iloc[i+1]['Activity'] == 0:
        c_1to0 +=1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I count how often a column value changes in a pandas dataframe

How do I remove/omit the count column from the dataframe in Pandas?

How to count a specific value in pandas dataframe column

How do I find the index of a known value in a pandas dataframe column?

How do I can filter pandas DataFrame by slice of column value

How do I append to a Pandas DataFrame column?

How do I take max value of a pandas dataframe column and find the corresponding value in another column?

How do I count how often a command was executed?

In Pandas, how do I create a dataframe from a count of items in a column that are separated by commas?

How do I count the total number of words in a Pandas dataframe cell and add those to a new column?

How to count value in pandas dataFrame

In a pandas dataframe, how can I filter the rows based on a column value, do calculation and assign the result to a new column?

How do I update the values in a pandas dataframe column until first occurance of a value in the same column?

How can I count the number of floats or integers in a column of a Pandas dataframe?

How do I get the value of a column in a Pandas DataFrame with the name based on another columns value on the same row?

How do I convert a pandas dataframe column of unique rows into separate column headings, count, and sum the adjacent row values?

Pandas: How do I get aggregate value changes between columns?

how do I get the count of rows in a appended/merged DataFrame in pandas?

How do I get the row count of a pandas DataFrame?

PostgreSQL how often does a value changes

How do I add a new column with a repeated value per group based on condition in a Pandas DataFrame?

How do I replace multiple different strings in Pandas dataframe column with one common string value?

How do I efficiently parse a substring in a Pandas Dataframe based on a value in a column?

How do I change a pandas row value, for a certain column, for a certrain date (datetimeindex) in a dataframe?

How do I add a column to a pandas dataframe which has the highest value in a range but applying it to every row?

How do I group by a column, and count values in separate columns (Pandas)

How do I get the row count of conditional value in DataFrame?

how to count, how often values from one column have a value in common in another column

How do I select distinct value and its count from a column?