-1

I have a dataframe containing a column of values (X).

df = pd.DataFrame({'X' : [2,3,5,2,2,3,7,2,2,7,5,2]})

For each row, I would like to find how many times it's value of X appears (A).

My expected output is:

enter image description here

R. Cox
  • 819
  • 8
  • 25

1 Answers1

1

create temp column with 1 and groupby and count to get your desired answer

df = pd.DataFrame({'X' : [2,3,5,2,2,3,7,2,2,7,5,2]})
df['temp'] = 1
df['count'] = df.groupby(['X'],as_index=False).transform(pd.Series.count)
del df['temp']
print(df)
tawab_shakeel
  • 3,701
  • 10
  • 26