poemsforaphrodite commited on
Commit
230aabc
1 Parent(s): fde6668

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -9
app.py CHANGED
@@ -295,12 +295,25 @@ def show_dimensions_selector(search_type):
295
  )
296
 
297
  def show_paginated_dataframe(report, rows_per_page=20):
298
- # Convert 'position' column to integer
299
  report['position'] = report['position'].astype(int)
 
300
 
301
  # Format CTR as percentage and relevancy_score with two decimal places
302
- report['ctr'] = report['ctr'].apply(lambda x: f"{x:.2%}")
303
- report['relevancy_score'] = report['relevancy_score'].apply(lambda x: f"{x:.2f}")
 
 
 
 
 
 
 
 
 
 
 
 
304
 
305
  # Create a clickable URL column
306
  def make_clickable(url):
@@ -319,14 +332,21 @@ def show_paginated_dataframe(report, rows_per_page=20):
319
  ascending = sort_order == "Ascending"
320
 
321
  # Convert back to numeric for sorting
322
- report['ctr'] = report['ctr'].str.rstrip('%').astype('float') / 100
323
- report['relevancy_score'] = report['relevancy_score'].astype('float')
 
 
 
 
 
 
324
 
325
- report = report.sort_values(by=sort_column, ascending=ascending)
 
 
326
 
327
- # Convert back to formatted strings for display
328
- report['ctr'] = report['ctr'].apply(lambda x: f"{x:.2%}")
329
- report['relevancy_score'] = report['relevancy_score'].apply(lambda x: f"{x:.2f}")
330
 
331
  total_rows = len(report)
332
  total_pages = (total_rows - 1) // rows_per_page + 1
 
295
  )
296
 
297
  def show_paginated_dataframe(report, rows_per_page=20):
298
+ # Convert 'position' column to integer and 'impressions' to numeric
299
  report['position'] = report['position'].astype(int)
300
+ report['impressions'] = pd.to_numeric(report['impressions'], errors='coerce')
301
 
302
  # Format CTR as percentage and relevancy_score with two decimal places
303
+ def format_ctr(x):
304
+ try:
305
+ return f"{float(x):.2%}"
306
+ except ValueError:
307
+ return x # Return the original value if it can't be converted to float
308
+
309
+ def format_relevancy_score(x):
310
+ try:
311
+ return f"{float(x):.2f}"
312
+ except ValueError:
313
+ return x # Return the original value if it can't be converted to float
314
+
315
+ report['ctr'] = report['ctr'].apply(format_ctr)
316
+ report['relevancy_score'] = report['relevancy_score'].apply(format_relevancy_score)
317
 
318
  # Create a clickable URL column
319
  def make_clickable(url):
 
332
  ascending = sort_order == "Ascending"
333
 
334
  # Convert back to numeric for sorting
335
+ def safe_float_convert(x):
336
+ try:
337
+ return float(x.rstrip('%')) / 100 if isinstance(x, str) and x.endswith('%') else float(x)
338
+ except ValueError:
339
+ return 0 # Return 0 or another default value if conversion fails
340
+
341
+ report['ctr_numeric'] = report['ctr'].apply(safe_float_convert)
342
+ report['relevancy_score_numeric'] = report['relevancy_score'].apply(safe_float_convert)
343
 
344
+ # Sort using the numeric columns
345
+ sort_column_numeric = sort_column + '_numeric' if sort_column in ['ctr', 'relevancy_score'] else sort_column
346
+ report = report.sort_values(by=sort_column_numeric, ascending=ascending)
347
 
348
+ # Remove the temporary numeric columns
349
+ report = report.drop(columns=['ctr_numeric', 'relevancy_score_numeric'])
 
350
 
351
  total_rows = len(report)
352
  total_pages = (total_rows - 1) // rows_per_page + 1