Quantcast
Channel: Silk Performer
Viewing all articles
Browse latest Browse all 4084

Forum Post: Python Pandas: Iterating a comparison between 2 DateTime columns from separate dataframes

$
0
0
Objective:  I have one dataframe with the following columns: AutoNumber, Amount, TransactionDate and GeneralLedgerType. I want to iterate over the unique AutoNumber IDs and subsequently split the data into a 'Debit' Table (GeneralLedgerType = 7) and 'Return' Table (GeneralLedgerType = 9). What I want to do is go through each of the Amounts in the 'Debit' Table and see if the absolute value corresponds to an equivalent amount in the 'Returns' Table, then check to see if the difference in the transaction dates between the 2 tables is less than 30 days. If so, then keep iterating until this conditional test fails and take that Transaction date. The code below is not taking the correct transaction dates. Code below: import pandas as pd import numpy as np data = pd.read_pickle('LoanHistory.pkl') data = data.set_index('TransactionDate').ix['2013-09-13':str(pd.to_datetime('today').date()+pd.Timedelta('1 day'))].reset_index() Unique_Loans = data.drop_duplicates('AutoNumber').AutoNumber AutoNumber = [] CurrentDate = [] for i in Unique_Loans: Debit = data[(data['GeneralLedgerType'] == 7) & (data['AutoNumber'] == i)][['AutoNumber', 'Amount', 'TransactionDate']].sort_values('TransactionDate', ascending=False).reset_index(drop=True) Return = data[(data['GeneralLedgerType'] == 9) & (data['AutoNumber'] == i)][['AutoNumber', 'Amount', 'TransactionDate']].sort_values( 'TransactionDate', ascending=False).reset_index(drop=True) if len(Debit.AutoNumber) == 0: AutoNumber.append("NA") CurrentDate.append("NA") elif len(Return.AutoNumber) == 0: AutoNumber.append(i) CurrentDate.append(Debit.TransactionDate[0]) elif (len(Debit.AutoNumber) 0) & (len(Return.AutoNumber) 0): if np.abs(Debit.AutoNumber.tolist()[0]) not in Return.AutoNumber.tolist(): AutoNumber.append(i) CurrentDate.append(Debit.TransactionDate[0]) else: for k in range(0, len(Return.Amount)): if (np.abs(Debit.Amount[k]) == Return.Amount[k]) & (Debit.TransactionDate[k] Return.TransactionDate[k]): AutoNumber.append(Debit.AutoNumber[k]) CurrentDate.append(Debit.TransactionDate[k]) break elif (np.abs(Debit.Amount[k]) == Return.Amount[k]) & (Return.TransactionDate[k] - Debit.TransactionDate[k] pd.Timedelta('30 days')): continue else: AutoNumber.append(Debit.AutoNumber[k]) CurrentDate.append(Debit.TransactionDate[k]) break df = pd.DataFrame({'AutoNumber': AutoNumber, 'CurrentDate': CurrentDate}) df.to_csv('output.csv')

Viewing all articles
Browse latest Browse all 4084

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>