I have two dataframes, I want to iterate over the elements in each list in the Companies column and match it with the company names in my second dataframe only if the date from the first dataframe occurs after the date of the second dataframe. I want two columns for the name matches and two columns for the date matches returned.
df = pd.DataFrame(columns=['Customer','Companies', 'Date'])
df = df.append({'Customer':'Gold', 'Companies':['Gold Ltd', 'Gold X', 'Gold De'], 'Date':'2019-01-07'}, ignore_index=True)
df = df.append({'Customer':'Micro', 'Companies':['Microf', 'Micro Inc', 'Micre'], 'Date':'2019-02-10'}, ignore_index=True)
Customer Companies Date
0 Gold [Gold Ltd, Gold X, Gold De] 2019-01-07
1 Micro [Microf, Micro Inc, Micre] 2019-02-10
df2 = pd.DataFrame(columns=['Companies', 'Date'])
df2 = df2.append({'Companies':'Gold Ltd', 'Date':'2019-01-01'}, ignore_index=True)
df2 = df2.append({'Companies':'Gold X', 'Date':'2020-01-07'}, ignore_index=True)
df2 = df2.append({'Companies': 'Gold De', 'Date':'2018-07-07'}, ignore_index=True)
df2 = df2.append({'Companies':'Microf', 'Date':'2019-02-18'}, ignore_index=True)
df2 = df2.append({'Companies':'Micro Inc', 'Date':'2017-09-27'}, ignore_index=True)
df2 = df2.append({'Companies':'Micre', 'Date':'2018-12-11'}, ignore_index=True)
Companies Date
0 Gold Ltd 2019-01-01
1 Gold X 2020-01-07
2 Gold De 2018-07-07
3 Microf 2019-02-18
4 Micro Inc 2017-09-27
5 Micre 2018-12-11
def match_it(d1, d2):
for companies in d1['Companies']:
for company in companies:
if d2['Companies'].str.contains(company).any():
mask = d1.Companies.apply(lambda x: company in x)
dff = d1[mask]
date1 = datetime.strptime(dff['Date'].values[0], '%Y-%m-%d').date()
date2 = datetime.strptime(d2[d2['Companies']==company]['Date'].values[0], '%Y-%m-%d').date()
if date2 < date1:
print(d2[d2['Companies']==company])
new_row = pd.Series([d2[d2['Companies']==company]['Date'], d2[d2['Companies']==company]['Companies']])
return new_row
Desired Output:
Customer Companies Date Name_1 Date_1 Name_2 Date_2
Gold [Gold Ltd, Gold X, Gold De] 2019-01-07 Gold Ltd 2019-01-01 Gold De 2018-07-07
Micro [Microf, Micro Inc, Micre] 2019-02-10 Micro Inc 2017-09-27 Micre 2018-12-11