Home > database >  How to add a new column with COUNTIFS using Pandas
How to add a new column with COUNTIFS using Pandas

Time:07-16

Can you please help me understand how I can do a create a separate column in a dataframe and store the value based on a COUNTIFS result.

I am having three columns currently in the dataframe Column 1 - Name of the Employee Column 2 - Name of the Manager (Can get repeated) Column 3 - Whether the employee is a Male / Female

I want to add a fourth column where I want to show the number of male employees corresponding to the Manager in Column 2.

I am still trying to learn how to append a table in Stackoverflow. So please excuse my lengthy query.

CodePudding user response:

Does this produces the desired result?

import pandas as pd

d = {
    'emp_name': ['foo', 'bar', 'abc'],
    'man_name': ['spam', 'spam', 'eggs'],
    'gender': ['F', 'M', 'F']
}

df = pd.DataFrame(d)

print(df)
"""
  emp_name man_name gender
0      foo     spam      F
1      bar     spam      M
2      abc     eggs      F
"""

out = df.groupby('man_name')['gender'].apply(lambda x: (x=='M').sum()).reset_index(name='count_if')

print(out)

"""
  man_name  count_if
0     eggs         0
1     spam         1
"""
  • Related