Search code examples
pythonpandasdataframeunique

Replace the unique values in a DataFrame column with their count


I have a DataFrame such as this:

Index Label
0     ABCD
1     EFGH
2     ABCD
3     ABCD
4     EFGH
5     ABCD
6     IJKL
7     IJKL
8     ABCD
9     EFGH

So, "ABCD" occurs 5 times, "EFGH" 3 times and "IJKL" twice. I want to count the occurence of each Label and replace the individual labels with their count, to get the following:

Index Label
0     5
1     3
2     5
3     5
4     3
5     5
6     2
7     2
8     5
9     3

What is the nicest way to do this? Thank you!


Solution

  • Use map by Series created by value_counts:

    df['Label'] = df['Label'].map(df['Label'].value_counts())
    print (df)
       Label
    0      5
    1      3
    2      5
    3      5
    4      3
    5      5
    6      2
    7      2
    8      5
    9      3
    

    Another solution with transform + size:

    df['Label'] = df.groupby('Label')['Label'].transform('size')
    print (df)
    
       Label
    0      5
    1      3
    2      5
    3      5
    4      3
    5      5
    6      2
    7      2
    8      5
    9      3