import pandas as pd # 1. Create the sample DataFrame data = { 'predicted_categories': [ ['80814001 - Freze Uçları', '13003106 - Freze', '80805004 - Sanayi Makineleri', '13003144 - Torna Makinesi', '13003195 - Kumpas'] ], 'pred_category_id': [80814001], 'text_predicted_probs': [ [0.943, 0.018, 0.008, 0.006, 0.004] ] } df = pd.DataFrame(data) # 2. Define a function to extract the probability matching the category ID def get_matching_prob(row): # Convert ID to string for matching target_id_str = str(row['pred_category_id']) # Iterate through the categories to find the matching index for index, category in enumerate(row['predicted_categories']): if category.startswith(target_id_str): # Return the corresponding probability from the same index return row['text_predicted_probs'][index] # Return None (or 0) if no match is found to prevent the code from crashing return None # 3. Create the new column df['pred_category_prob'] = df.apply(get_matching_prob, axis=1) # Display the result print(df[['pred_category_id', 'pred_category_prob']])