python

make algoritm of generation faster, more memory efficient,  use dict



import pandas as pd
import re

# Reading data into DataFrames
data1 = {
    'idf': [22, 44, 55, 443],
    'category': [114, 114, 116, 114],
    'formula': ['predlozheniya_uslug/{716}/{719}', 'predlozheniya_uslug/{716}/{720}', 'predlozheniya/{716}/{720}', 'predlozheniya/{716}/{716}/{720}']
}
DF1 = pd.DataFrame(data1)

data2 = {
    'param_id': [716, 716, 716, 716, 716, 716, 719, 719, 719, 719, 719, 719, 719, 720],
    'slug': ['it_internet_telekom', 'nyani_sidelki', 'krasota_zdorove', 'bytovye_uslugi', 'delovye_uslugi', 'oborudovanie_proizvodstvo',
             'buhgalteriya_finansy', 'konsultirovanie', 'nabor_i_korrektsiya_teksta', 'perevod', 'yuridicheskie_uslugi', 
             'buhgalteriya_finansy', 'konsultirovanie', 'translation_services'],
    'addItemLabel': ['IT, маркетинг', 'Няни, сиделки', 'Красота, здоровье', 'Бытовые услуги', 'Деловые услуги', 
                     'Оборудование, производство', 'Бухгалтерия, финансы', 'Консультирование', 
                     'Набор и коррекция текста', 'Перевод', 'Юридические услуги', 'Бухгалтерия, финансы', 'Консультирование',
                     'Translation services'],
    'value': ['MTAxOTU=', 'MTAxOTY=', 'MTAxOTc=', 'MTAyMDA=', 'MTAyMDE=', 'MTAyMDI=', 'MTAyMzU=', 'MTAyMzY=', 'MTAyMzg=', 
              'MTAyMzk=', 'MTAyNDE=', 'MTAyMzU=', 'MTAyMzY=', 'MTAyMzk=']
}
DF2 = pd.DataFrame(data2)
#DF2 =pd.read_csv('parameters_1.csv')
#DF3 =pd.read_csv('parameter_child_parent.csv')
#DF1 =pd.read_csv('formula.csv')

data3 = {
    'category': [114, 114, 114, 114, 114, 114],
    'parent_value': ['MTAyMDE=', 'MTAyMDE=', 'MTAyMDE=', 'MTAyMDE=', 'MTAyMDE=', 'MzE1NTIwOA=='],
    'value': ['MTAyMzg=', 'MTAyMzk=', 'MTAyMzU=', 'MTAyMzY=', 'MTAyNDE=', 'MTAyNDE=']
}
DF3 = pd.DataFrame(data3)
DF3 =pd.read_csv('parameter_child_parent.csv')

# Initialize an empty DataFrame to store the results
result_df = pd.DataFrame(columns=['idf', 'category', 'formula', 'slug', 'value', 'addItemLabel', 'parent_value'])
result_list = []

# Process each row in DF1
for index, row in DF1.iterrows():
    formula = row['formula']
    category = row['category']
    
    # Find all the parameter ids in the formula using regex
    param_ids = re.findall(r'{(\d+)}', formula)
    
    replacements = {}
    values = []
    slugs = []
    add_item_labels = []
    parent_values = []
    
    # Replace each param_id in the formula with the respective slug
    for param_id in param_ids:
        param_id = int(param_id)  # Convert param_id to integer
        param_data = DF2.loc[DF2['param_id'] == param_id, ['slug', 'value', 'addItemLabel']]
        
        if not param_data.empty:
            slug = param_data.iloc[0]['slug']
            value = param_data.iloc[0]['value']
            add_item_label = param_data.iloc[0]['addItemLabel']
            replacements[param_id] = slug
            values.append(value)
            slugs.append(slug)
            add_item_labels.append(add_item_label)

    matches = []
    if values:
        # Filter DF3 based on category matching and values within the same row
        for _, df3_row in DF3.iterrows():
            if df3_row['category'] == category and df3_row['value'] in values:
                matches.append(df3_row['value'])
                parent_values.append(df3_row['parent_value'])
    
    if matches:
        for param_id, slug in replacements.items():
            formula = formula.replace(f'{{{param_id}}}', slug)
        result_list.append({
            'idf': row['idf'],
            'category': category,
            'formula': formula,
            'slug': ', '.join(slugs),
            'value': ', '.join(values),
            'addItemLabel': ', '.join(add_item_labels),
            'parent_value': ', '.join(parent_values)
        })

# Convert the result list to a DataFrame and concat with the result_df
if result_list:
    result_df = pd.concat([result_df, pd.DataFrame(result_list)], ignore_index=True)

# Print the resultant DataFrame
 

display(result_df)
display(result_df.iloc[0,2])