lamba x sum if df1[col_x] based on multiple column matching criteria across 2 dfs : I have some time stampled financial data as shown in the image.[Sample Data][1]

For companies that have had transaction B(s), I want to calculate sum of transaction A(s) values that occurred prior to transaction B. Here's my code so far:


    companies_with_trans_B = all_companies[all_companies['transaction_type'] == 'B']
   

    companies_with_trans_B['sum_prior_trans_A'] = companies_with_trans_B.apply(lambda x: all_companies['transaction_size_USDmm'].sum() if (all_companies['target_company_name'] == companies_with_trans_B['target_company_name'] and all_companies['transaction_type'] == 'A' and all_companies['transaction_announced_date'] <= companies_with_trans_B['transaction_announced_date']) else math.nan)

Receive the following error: "ValueError: ('Can only compare identically-labeled Series objects', 'occurred at index target_company_name')"

Is there a way to achieve this without using lambda x function?

  [1]: https://i.stack.imgur.com/FT3bs.jpg