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 ?
>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