Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Account Number Count

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading