1

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 ?

3
  • Will you please add a sample dataframe containing your expected output? Commented Jan 30, 2022 at 18:44
  • You are probably looking for size or sum Commented Jan 30, 2022 at 18:45
  • and transform. Commented Jan 30, 2022 at 18:45

2 Answers 2

4

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 
Sign up to request clarification or add additional context in comments.

2 Comments

does using map slow down the process anyway
@gilf0yle I'm not really sure...I once saw a post where replace was faster than map, but I'm not sure about here. Anyway, your answer is good, I'll upvote it tomorrow since I'm out of votes :|
1

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.