【对比Python】外键关联1
任务:找出员工中夫妻工资和超过10000的员工
Python
1 | import pandas as pd |
2 | emp_file = "E:\\txt\\Employees.txt" |
3 | rel_file = "E:\\txt\\EmpRel.txt" |
4 | emp_info = pd.read_csv(emp_file,sep='\t') |
5 | rel_info = pd.read_csv(rel_file,sep='\t') |
6 | rel_info = rel_info[rel_info['Relationship']=="Spouse"] |
7 | emp1_join = pd.merge(rel_info,emp_info,left_on='Emp1',right_on='ID') |
8 | emp2_join = pd.merge(emp1_join,emp_info,left_on='Emp2',right_on='ID') |
9 | res = emp2_join[emp2_join['BasePay_x']+emp2_join['BasePay_y']>=10000] |
10 | r = res[['Name_x','Name_x']] |
11 | print(r) |
Pandas只能进行两次连接得到两列的信息,而后进行筛选。
集算器
A | |
1 | =file("E:\\txt\\Employees.txt").import@t().keys(ID) |
2 | =file("E:\\txt\\EmpRel.txt").import@t() |
3 | =A2.select(Relationship=="Spouse") |
4 | >A3.switch(Emp1,A1;Emp2,A1) |
5 | =A3.select(Emp1.BasePay+Emp2.BasePay>=10000) |
6 | >A5.run(Emp1=Emp1.Name,Emp2=Emp2.Name) |
集算器通过外键对象化,把外键字段转换成外键表中对应的引用,这样就可以当单表来处理了。