使用以下规范创建一个函数:
功能名称:employee_matcher
目的:将员工姓名与提供的公司,年龄和性别相匹配
参数:company(string),age(int),gender(string)
返回:员工first_name和last_name,如下所示:return first_name,last_name
注意:如果有多个员工符合相同的描述,则first_name和last_name应返回所有可能的名字和姓氏的列表
df_employee = pd.read_json(open('employee_info.json'))
print(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
这是我的尝试。
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)
请注意,打印(匹配)将打印出以下输出
0 False
1 False
2 False
3 False
4 False
5 False
6 False
7 True
8 False
9 False
10 False
employee_matcher('1und1', 36, 'Female')
此行的示例运行将打印出Kylynn Vedikhov
答案 0 :(得分:1)
这里的问题是匹配是一个掩码,您需要使用掩码来查找满足条件的行。即onViewCreated()
这是修改后的代码
df_employee[mask]
该函数将返回匹配记录的所有名字/姓氏
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()