Didn't use any complex ways or algorithm ;-)
merge
thesales_person
andorders
, usinghow='outer'
to match the salesman and the order they made. The result table isa
.merge
thea
andcompany
, usinghow='left'
to match the salesman, order and company. The result table isb
.- We only need salesman's name(which is
b
'sname_x
) and company's name(which isb
'sname_y
). Filter these two and the result isc
. name_red
is salemans who negotiated any order withRED
. Filter them.merge
c
andname_red
and getd
. Now, inname_y_y
column ofd
, every salesman who have ever made a order withRED
, has aRED
in his/her row.- Filter those who doesn't have
RED
and create a new DataFrame. Don't forgetdrop_duplicates()
when outputing.
import pandas as pd
def sales_person(sales_person: pd.DataFrame, company: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
a=pd.merge(
sales_person,orders,how='outer',on='sales_id'
)
b=pd.merge(
a,company,how='left',on='com_id'
)
c=b.loc[:,['name_x','name_y']]
name_red=c[c['name_y'] == 'RED']
d=pd.merge(
c,name_red,how='outer',on='name_x'
)
result=pd.DataFrame({'name':d[d['name_y_y'] != 'RED']['name_x']})
return result.drop_duplicates()