2021年1月25日星期一

Combine dataframes to allocate lenders to borrowers, in pandas

I have two dataframes: lenders and borrowers. I want to combine values from lenders to borrowers, as below.

LenderDF = pd.DataFrame({  'LenderID':['Lender 1', 'Lender 2','Lender 3','Lender 4', 'Lender 5'],  'AmountHave': [300, 150, 500, 100, 200]  })  borrowerDF = pd.DataFrame({  'borrowerID': ['Borrower 1', 'Borrower 2','Borrower 3','Borrower 4'],  'AmountNeed': [100,200,150,300]   })    print(LenderDF)  print(borrowerDF)       LenderID  AmountHave  0  Lender 1         300  1  Lender 2         150  2  Lender 3         500  3  Lender 4         100  4  Lender 5         200       borrowerID  AmountNeed  0  Borrower 1         100  1  Borrower 2         200  2  Borrower 3         150  3  Borrower 4         300  

The final output I'm trying to get is:

   BorrowerID  AmountNeeded  LenderID  LenderHave(after lending amount)  RemainingAmount  0  Borrower 1           100  Lender 1                                     300              200  1  Borrower 2           200  Lender 1                                     200                0  2  Borrower 3           150  Lender 2                                     150                0  3  Borrower 4           300  Lender 3                                     500              200  

Example:

  • First lender has 300 so they can lend money to (say) Borrower 1 and Borrower 2:
  • 300(firstLender) >= 100 (firstBorrower Amount needed)
  • So assign or set value of First lender "Lender 1" to the Borrower who took the money and set the remaining value 200 First value of AmountLenderHave will be 300 since this is the first time that person is lending amount.
  • 200(firstLender) >= 200 (secondBorrower amount needed)

Likeise, iterate on each lender row and so on, until no lender is remaining.

https://stackoverflow.com/questions/65891989/combine-dataframes-to-allocate-lenders-to-borrowers-in-pandas January 26, 2021 at 04:27AM

没有评论:

发表评论