python check the algoritm. It's very slow because of all_combinations =…
- User
python
check the algoritm. It's very slow because of all_combinations = product(*[get_param_data(pid) for pid in param_ids]). Try to make it faster
import pandas as pd import re from itertools import product
Load the CSV files
url_base_infm = pd.read_csv('url_base_infm.csv') url_base_infm.drop_duplicates(subset='url', inplace=True)
DF2 = pd.read_csv('parameters_all.csv') DF3 = pd.read_csv('parameter_child_parent.csv') DF1 = pd.read_csv('formula0.csv')
pattern = r'{([^{}]*)}'
Initialize an empty list to store the extracted values
control_values_list = []
Iterate through each row in the DataFrame
for index, row in DF1.iterrows(): formula = row['formula']
code# Find all matches of the pattern in the formula matches = re.findall(pattern, formula) # Append the matched values to the list control_values_list.extend(matches)#display(control_values_list)
Transform the extracted values to integers
control_values_list = [int(value) for value in control_values_list]
#display(DF2.loc[DF2['param_id']==720]) #display(DF2.loc[DF2['param_id'].isin(control_values_list)]) DF2 = (DF2.loc[DF2['param_id'].isin(control_values_list)])
Initialize a list to store the results
result_list = []
Creating dictionaries for quick lookup
param_id_to_data = DF2.groupby('param_id').apply(lambda x: x.to_dict('records')).to_dict()
Group by category for quick lookup of parent-child value pairs
category_to_pair_values = DF3.groupby('category').apply(lambda x: list(zip(x['parent_value'], x['value']))).to_dict() print('Data loaded:')
Function to get all relevant data for a parameter id
def get_param_data(param_id): return param_id_to_data.get(param_id, [])
def get_param_data_id(param_id): return param_id_to_data.get(id, [])
#id param_id
Function to append the result
def append_result(idf, category, formula_pattern, param_ids, combination): formula = formula_pattern formula_pattern_start = formula_pattern slugs = [] values = [] add_item_labels = []
code - Assistant
- User
for combination in valid_combinations: append_result(idf, category, formula_pattern, param_ids, combination)
TypeError: sequence item 0: expected str instance, float found
- Assistant