Untitled
unknown
plain_text
a year ago
2.8 kB
11
Indexable
# Function to trace back parents and find corresponding END-IFs
def trace_back_parents_with_end_if(self, df, sequence_number):
# df = df.reset_index()
# print("im in block")
# print(df['Sequence'][0])
# print(type(df['Sequence'][0]))
# print("Im sequence : ", type(sequence_number))
print("\n\n\n\n\nORIGINAL DF\n\n\n")
for i, row in df.iterrows():
print()
print(row)
print()
# Initialize result list
result_rows = []
end_if_rows = []
# Start with the current sequence
current_seq = sequence_number
print("\n\nCURRENT SEQUENCE : \n\n",current_seq)
# Loop to find parent sequences (IF statements)
while True:
# Find the row corresponding to the current sequence
row = df[df['Sequence'] == str(current_seq)]
print("Found the row :", row)
# If no row is found, break the loop
if row.empty:
break
# Add the row to the result
result_rows.append(row)
# Get the Conditional_Seq value of the current row
org_seq = row['Conditional_Seq'].values[0]
print("\n\n\n\nORG SEQ :",org_seq)
print("Type:",type(org_seq))
if pd.isna(org_seq):
break
else:
cond_seq = int(float(org_seq))
print("Conditional sequence of row :", cond_seq)
print("Conditional sequence of row :", type(cond_seq))
# If there's no parent (Conditional_Seq is None), stop
if pd.isna(cond_seq):
break
# Move to the parent sequence
current_seq = cond_seq
print("Printing Parent:", current_seq)
# Concatenate all result rows (IF statements)
result_df = pd.concat(result_rows).sort_values(by='Sequence')
# Now we find the corresponding END-IF statements
for index, if_row in result_df.iterrows():
if_seq = str(if_row['Sequence'])
# Find the END-IF that matches this IF sequence
end_if_row = df[(df['Keywords'].str.startswith('END')) &
(int(float(df['Conditional_Seq'])) == if_seq)]
if not end_if_row.empty:
end_if_rows.append(end_if_row)
# Concatenate all result rows (including IF and END-IF)
if end_if_rows:
end_if_df = pd.concat(end_if_rows)
final_df = pd.concat([result_df, end_if_df]).sort_values(by='Sequence')
else:
final_df = result_df
print("\n\n\n\n\n\n\n\n\nPRINTING DF : \n\n", final_df)
return final_dfEditor is loading...
Leave a Comment