Untitled
unknown
plain_text
14 days ago
2.1 kB
2
Indexable
Never
def get_immediate_parent_children(self, df): """ Function to get immediate parent and children of target variable """ # logging(f"Start - Home - Get Parent and Child") try: #Get BRE_ATTRIBUTE TBL Data for program conn = sqlite3.connect(os.getenv('SQLITE_DB_PATH')) df['Parents'] = '' df['Children'] = '' print(df.columns) filter_prg = "', '".join(df['Program_Name'].unique().tolist()) program_df = pd.read_sql_query(sql=f"""SELECT * FROM BRE_ATTRIBUTE_TBL WHERE PROGRAM_NAME in ('{filter_prg}')""",con=conn).drop_duplicates() print(program_df.columns) conn.close() for index, row in df.iterrows(): var_name = row['Target'] program_id = row['Program_Name'] parent_data = program_df[(program_df['VARIABLE_NAME']==var_name) & (program_df['VARIABLE_PARENT']!='N/A') & (program_df['PROGRAM_NAME']==program_id)]['VARIABLE_PARENT'] immediate_parent = parent_data.iloc[0] if not parent_data.empty else "" print("IMMEDIATE PARENT : ",immediate_parent) child_data = program_df[(program_df['VARIABLE_PARENT']==var_name) & (program_df['VARIABLE_NAME']!='N/A') & (program_df['PROGRAM_NAME']==program_id)]['VARIABLE_PARENT'] immediate_child = child_data.iloc[0] if not child_data.empty else "" print("IMMEDIATE CHILD : ",immediate_child) parent_exists = df.loc[df['Program_Name'] == program_id, 'Target'].str.contains(immediate_parent).any() child_exists = df.loc[df['Program_Name'] == program_id, 'Target'].str.contains(immediate_child).any() df.loc[index, 'Parents'] = immediate_parent if parent_exists else '' df.loc[index, 'Children'] = immediate_child if child_exists else '' print(df.iloc[0]) # logging(f"Completed - Home - Get Parent and Child") return df
Leave a Comment