python make algoritm work faster _________ %%time import pandas as pd from itertools…
- User
python
make algoritm work faster
%%time
import pandas as pd from itertools import product import random import string def generate_random_id(length=10): return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))
Sample DataFrame DF1
data = { 'idx': [1, 2], 'text1': ['{" ,"value":"MzI5MTkz"},XXXXX}]}', '{" " ,"value":"MzI5MTkz"},XXXXX}]}'] } #df1 = pd.DataFrame(data)
Lists of car brands and models
Create a list of all combinations of brands and models using itertools.product
brand_model_combinations = list(product(list1, list2))
Create an empty list to store the resulting DataFrames
result_dfs = []
Iterate through each row in DF1
for index, row in df1.iterrows(): text1 = row['text1']
code# Iterate through each brand and model combination for brand, model in brand_model_combinations: # Create a new DataFrame for each combination new_text = text1.replace('GGGGGG', brand).replace('XXXXYYY', model) new_df = pd.DataFrame({'idx': [row['idx']], 'text1': [text1], 'brand': [brand], 'model': [model], 'text': [new_text]}) result_dfs.append(new_df)Concatenate all resulting DataFrames into df_result
df_result = pd.concat(result_dfs, ignore_index=True)
Display the resulting DataFrame df_result
df_result.drop_duplicates(subset=['text'], inplace=True)
#df_result = pd.merge(df_result, fff1 , how='left', left_on='brand' , right_on='text' ) df_result = pd.merge(df_result, fff [[ 'text', 'brand', 'model' ]], how='left', left_on='model' , right_on='text' )
random_id = generate_random_id() random_id3 = generate_random_id()
file_name = f"./url_formula/auto_formula_{random_id}_{random_id3}.csv"
df_result.to_csv(file_name, index=False) print(df_result.shape) display(df_result.sample(22))
- Assistant
- User
how to save memory ?
maybe make drop_duplicates in the middle of process ?
- Assistant