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

Counting values

I work with Pandas data frame. I want to count data by one column. You can see example below:

data = {'name': ['Company1', 'Company2', 'Company1', 'Company2', 'Company1'], 
        'employee': [1, 1, 1, 1, 1], 
       }
df = pd.DataFrame(data, columns = ['name', 'employee'])
df

Now I want to count all employees from column employee into new column. I try to do this with this line of code

 df['employees']=df.groupby(['name']).agg({'employee':np.count,})

But after execution this line of code I got this error:

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

module 'numpy' has no attribute 'count'

So can anybody help me how to solve this problem ?

>Solution :

Are you looking for something like this?

df['employee'] = df.groupby('name')['employee'].transform('count')

Output:

>>> df
       name  employee
0  Company1         3
1  Company2         2
2  Company1         3
3  Company2         2
4  Company1         3
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