I am reaching out to see if there is a way to count the number of times an account number or other key shows up in a dataframe and then place that count in the row with the account number. I know there is the value_counts() function but that is not what I am wanting to do.
This is what I am wanting to achieve. The count column is what I am wanting to append to the dataframe:
account count
0 456 3
1 123 4
2 258 2
3 456 3
4 123 4
5 123 4
6 258 2
7 456 3
8 123 4
>Solution :
You can do that with either of the following.
df["count"] = df['account'].map(df['account'].value_counts())
#32ms on 1 million rows
df["count"] = df.groupby(['account'])['account'].transform('count')
#52ms on 1 million rows