Rod Efraim
Rod Efraim

Reputation: 65

Python: Match employee name with company, age, gender

Create a function with the following specifications:

Function name: employee_matcher

Purpose: to match the employee name with the provided company, age, and gender

Parameter(s): company (string), age (int), gender (string)

Returns: The employee first_name and last_name like this: return first_name, last_name

Note: If there are multiple employees that fit the same description, first_name and last_name should return a list of all possible first names and last name

df_employee = pd.read_json(open('employee_info.json'))
  print(df_employee)

Elements in df_employee

     age      company first_name  gender        last_name
0     42      123-reg  Inglebert    Male         Falconer
1     14          163     Rafael    Male         Bedenham
2     31          163     Lemuel    Male             Lind
3     45          163      Penny  Female          Pennone
4     52          163       Elva  Female         Crighton
5     55         1688   Herminia  Female            Sisse
6     30        1und1       Toby  Female           Nisuis
7     36        1und1     Kylynn  Female         Vedikhov
8     37        1und1     Mychal    None          Denison
9     32          360    Angelle  Female           Kupisz
10    35          360     Ilario    Male          Mannagh

Code

This is my attempt.

def employee_matcher(company, age, gender):
  match = (df_employee['company'] == company) & (df_employee['age'] == age) & (df_employee['gender'] == gender)

  print(match)

  return list(pd.Series(match['first_name']).values, pd.Series(match['last_name']).values)

Note that the print(match) will print out the following output

0      False
1      False
2      False
3      False
4      False
5      False
6      False
7      True
8      False
9      False
10     False

Example Run

employee_matcher('1und1', 36, 'Female')

Example run would be for this line to print out Kylynn Vedikhov

Upvotes: 1

Views: 1762

Answers (1)

sgDysregulation
sgDysregulation

Reputation: 4417

The problem here is that match is a mask, you'll need to use the mask to find the rows that satisfy the conditions. i.e. df_employee[mask]

here is the modified code

def employee_matcher(company, age, gender):
    match = df_employee[(df_employee['company'] == company) & (
        df_employee['age'] == age) & (
        df_employee['gender'] == gender)]
    return (match.first_name + ' ' + match.last_name).values.tolist()

the function will return all the first-name/last-names of matching records

In []: employee_matcher('1und1', 36, 'Female')
Out[]: ['Kylynn Vedikhov']

Upvotes: 1

Related Questions