Add a new column with counter to a df based on an existing column

The data frame I have:

Column A Column B
A 1
na 4
na 5
na 6
B 2
na 4
na 6
na 7
na 8
C 6
na 1
na 5

I am trying to loop through data frame using Python and create a new column C based on Column A's value. Output should look like this;

Column A Column B Column C
A 1 1
na 4 1
na 5 1
na 6 1
B 2 2
na 4 2
na 6 2
na 7 2
na 8 2
A 6 3
na 1 3
na 5 3

Basically adding a counter in column C when there is a new value in Column A after NAs(even if the value in Column A is same as the previous value; in this eg. A comes twice but the counter gives 1st A value 1 and when it again comes then it gives it a value 3).



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation