Python Pandas Column Value Increment Counter for Unique Values

Question:

Say I have a series like (assume this is a dataframe with many columns, but I only care about df["Key"] right now) :

Key
----
1234
1234
1234
5678
6789
7890
7890
6789
2345

How do I create a new column called "Counter" that increments matching values in "Key" ?, like the following :

Key     Counter
----    -------
1234       1
1234       2
1234       3
5678       1
6789       1
7890       1
7890       2
6789       2
2345       1

I don’t want a summary of the total count of each unique value…I know that you can get a value of unique counts by doing something like df["Key"].value_counts() or df.groupby('Key').count()

Asked By: smichael_44

||

Answers:

Use pd groupby cumcount:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.GroupBy.cumcount.html

which maps to 0-n, so optionally add 1 to the result:

out = df.groupby('Key').cumcount() + 1
Answered By: Chrysophylaxs

Try:

df["Counter"] = df.groupby("Key").cumcount().reset_index(drop=True) + 1

this will produce your expected result:

    Key  Counter
0  1234        1
1  1234        2
2  1234        3
3  5678        1
4  6789        1
5  7890        1
6  7890        2
7  6789        2
8  2345        1
Answered By: Wiliam
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.