python make a randomizing fast algoritm Use process control - fill total result 'newurl'…
- User
python
make a randomizing fast algoritm
Use process control - fill total result 'new_url' with minimum 25 distributions of url, don't make 'new_url' with <5 distributions
from collections import defaultdict import random import pandas as pd
Read CSV files into DataFrames
imm_all_final = pd.read_csv('seo_tags_infm_matching2408.csv.gz', nrows=4444) im_randomizer = pd.read_csv('im_randomizer26.csv.gz') imm_all_final ['priority'] = 50
Preprocess the 'base' and 'random' columns in im_randomizer
im_randomizer['base'] = im_randomizer['base'].astype(str)
Drop duplicates in the im_randomizer DataFrame
im_randomizer.drop_duplicates(inplace=True)
Create a dictionary for fast lookup
im_randomizer_dict = im_randomizer.groupby('base')['random'].apply(list).to_dict()
def merge_data_randomizer2(row, im_randomizer_dict): # Retrieve the 'infm' value from the DataFrame row infm_value = row['infm']
code# Get matching values from the dictionary or an empty list if no match is found matching_values = im_randomizer_dict.get(infm_value, []) # Return a random choice if there are matching values, otherwise return the original infm_value return random.choice(matching_values) if matching_values else infm_valueFunction to update new_url for rows with 'priority' less than 25
def update_new_url(imm_all_final): # Assign random url or infm to new_url based on priority for index, row in imm_all_final.iterrows(): if row['priority'] < 2555555555555555: imm_all_final.at[index, 'new_url'] = merge_data_randomizer2(row, im_randomizer_dict) else: imm_all_final.at[index, 'new_url'] = row['infm'] if index % 100000 == 0: display(f"Processed {index} rows")
Update DataFrame
update_new_url(imm_all_final)
Display a sample portion of the updated DataFrame
display(imm_all_final.sample(22))
- Assistant
- User
there are still urls with 1-5 distributed urls in result let's try to fix them
- Assistant