Home > database >  Pandas COUNTIF equivalent (preserve duplicate values, see description)
Pandas COUNTIF equivalent (preserve duplicate values, see description)

Time:11-17

I have the following pandas column1 and I want to create a column2 displaying the count of the value in each row in the column1.

I do not want to use pandas value_counts as I do not want to group by the values of the column. Column1 :

COL 1   
VALUE1  
VALUE2
VALUE1
VALUE1
VALUE1
VALUE3
VALUE2
VALUE1
VALLUE3
VALUE2

Desired result :

COL 1   Desired Result
VALUE1  5
VALUE2  3
VALUE1  5
VALUE1  5
VALUE1  5
VALUE3  1
VALUE2  3
VALUE1  5
VALLUE3 1
VALUE2  3

CodePudding user response:

value_counts does not require you to group and it creates a series which you can map back to your df:

df['Resired Result'] = df['COL 1'].map(df['COL 1'].value_counts())

prints

     COL 1  Resired Result
0   VALUE1               5
1   VALUE2               3
2   VALUE1               5
3   VALUE1               5
4   VALUE1               5
5   VALUE3               1
6   VALUE2               3
7   VALUE1               5
8  VALLUE3               1
9   VALUE2               3

CodePudding user response:

value_counts might be more efficient, but you can also achieve it with groupby.transform('count'):

df['Resired Result'] = df.groupby('COL 1')['COL 1'].transform('size')

Output:

     COL 1  Resired Result
0   VALUE1               5
1   VALUE2               3
2   VALUE1               5
3   VALUE1               5
4   VALUE1               5
5   VALUE3               1
6   VALUE2               3
7   VALUE1               5
8  VALLUE3               1
9   VALUE2               3
  • Related