fschwartzer commited on
Commit
9b0a7bb
1 Parent(s): 074db95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -64,22 +64,21 @@ def calcular_fator_avaliacao(titulo, EC, PU):
64
  fator_avaliacao = max((4 * ec_pontuacao + 6 * PVU - 3 * PUB) / 100, VR)
65
  return fator_avaliacao
66
 
67
- def select_nearest_items(df, n=5):
68
  median_price = df['Price'].median()
69
  df['Distance'] = (df['Price'] - median_price).abs()
70
- df_sorted = df.sort_values(by=['Distance', 'Marketplace']) # Sort by distance then by marketplace to diversify
71
- selected_items = pd.DataFrame()
72
-
73
- marketplaces_seen = set()
 
74
  for _, row in df_sorted.iterrows():
75
- if len(selected_items) >= n:
 
 
 
76
  break
77
- if row['Marketplace'] not in marketplaces_seen:
78
- selected_items = selected_items.append(row, ignore_index=True)
79
- marketplaces_seen.add(row['Marketplace'])
80
-
81
- selected_items = selected_items.drop(columns=['Distance']) # Remove the temporary 'Distance' column
82
- return selected_items
83
 
84
  def integrated_app(query, titulo, EC, PU):
85
  df_mercadolibre = fetch_data_to_dataframe(query, 50, "mercadolibre")
 
64
  fator_avaliacao = max((4 * ec_pontuacao + 6 * PVU - 3 * PUB) / 100, VR)
65
  return fator_avaliacao
66
 
67
+ def select_nearest_items(df):
68
  median_price = df['Price'].median()
69
  df['Distance'] = (df['Price'] - median_price).abs()
70
+ df_sorted = df.sort_values('Distance')
71
+
72
+ # Ensuring unique marketplaces, maintaining a list of included marketplaces
73
+ included_marketplaces = set()
74
+ nearest_items = pd.DataFrame()
75
  for _, row in df_sorted.iterrows():
76
+ if row['Marketplace'] not in included_marketplaces:
77
+ nearest_items = nearest_items.append(row, ignore_index=True)
78
+ included_marketplaces.add(row['Marketplace'])
79
+ if len(included_marketplaces) == 5:
80
  break
81
+ return nearest_items
 
 
 
 
 
82
 
83
  def integrated_app(query, titulo, EC, PU):
84
  df_mercadolibre = fetch_data_to_dataframe(query, 50, "mercadolibre")