Home > OS >  Counting values
Counting values

Time:01-31

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:

module 'numpy' has no attribute 'count'

So can anybody help me how to solve this problem ?

CodePudding user response:

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

CodePudding user response:

My answer is based on my understanding of the question. please be clear and crisp from next time you ask a question. initial Dataframe:

       name  employee
0  Company1         1
1  Company2         1
2  Company1         1
3  Company2         1
4  Company1         1

use this

df['employees']=df['name'].map(df.name.value_counts().to_dict())

output:-

       name  employee  employees
0  Company1         1         3
1  Company2         1          2
2  Company1         1          3
3  Company2         1          2
4  Company1         1          3
  •  Tags:  
  • Related