PraneshJs commited on
Commit
2a2bd75
Β·
verified Β·
1 Parent(s): ca1f1d0

calculated year average by own

Browse files
Files changed (1) hide show
  1. app.py +66 -2
app.py CHANGED
@@ -592,7 +592,7 @@ def get_detailed_student_points(roll_no, studentwise_data):
592
  student_data[header] = student_found[i] if i < len(student_found) else ""
593
 
594
  output = []
595
- output.append("\n")
596
  output.append("πŸ† REWARD POINTS BREAKDOWN")
597
  output.append("=" * 80)
598
 
@@ -646,6 +646,65 @@ def get_detailed_student_points(roll_no, studentwise_data):
646
 
647
  return "\n".join(output)
648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
649
  # Load initial data
650
  print("πŸ“Š Loading initial data...")
651
  load_all_data()
@@ -794,7 +853,7 @@ def get_system_info():
794
  output.append("=" * 80)
795
  output.append("SYSTEM INFORMATION")
796
  output.append("=" * 80)
797
-
798
  # Average Points
799
  if 'average_points' in details_info:
800
  output.append("\nAVERAGE REWARD POINTS BY YEAR:")
@@ -802,6 +861,11 @@ def get_system_info():
802
  for year, points in details_info['average_points'].items():
803
  if points:
804
  output.append(f"Year {year:<10}: {points}")
 
 
 
 
 
805
 
806
  # Redemption Dates
807
  if 'ip1_redemption' in details_info:
 
592
  student_data[header] = student_found[i] if i < len(student_found) else ""
593
 
594
  output = []
595
+ output.append("")
596
  output.append("πŸ† REWARD POINTS BREAKDOWN")
597
  output.append("=" * 80)
598
 
 
646
 
647
  return "\n".join(output)
648
 
649
+ def calculate_yearwise_average_points():
650
+ """Calculate year-wise average points from the combined data"""
651
+ combined_df, _, _, _ = get_cached_data() # FIX: Get all 4 return values
652
+ if combined_df.empty:
653
+ return "❌ No data available to calculate averages"
654
+
655
+ # Try to find columns automatically
656
+ year_col, points_col = None, None
657
+ for col in combined_df.columns:
658
+ if 'year' in col.lower():
659
+ year_col = col
660
+ if 'balance' in col.lower() and 'points' in col.lower():
661
+ points_col = col
662
+
663
+ if not year_col or not points_col:
664
+ return "⚠️ Required columns not found in the data"
665
+
666
+ # Create a copy for processing
667
+ df = combined_df[[year_col, points_col]].copy()
668
+
669
+ # Clean and convert points data
670
+ df[points_col] = pd.to_numeric(
671
+ df[points_col].astype(str).str.replace(',', '').str.strip(),
672
+ errors='coerce'
673
+ )
674
+ df.dropna(subset=[points_col], inplace=True)
675
+
676
+ # Remove rows with zero or negative points for more accurate averages
677
+ df = df[df[points_col] > 0]
678
+
679
+ if df.empty:
680
+ return "⚠️ No valid points data found for calculation"
681
+
682
+ # Group by year
683
+ yearwise = df.groupby(year_col)[points_col].agg(['sum', 'count', 'mean', 'min', 'max']).reset_index()
684
+ yearwise['average'] = yearwise['mean'] # Use pandas mean for consistency
685
+
686
+ # Format the output neatly
687
+ output = []
688
+ output.append("=" * 90)
689
+ output.append(" ")
690
+ output.append("πŸ“Š YEAR-WISE AVERAGE REWARD POINTS (CALCULATED)")
691
+ output.append("-" * 90)
692
+
693
+ for _, row in yearwise.iterrows():
694
+ year = str(row[year_col]).strip()
695
+ total_points = f"{row['sum']:.0f}"
696
+ count = int(row['count'])
697
+ avg = f"{row['average']:.2f}"
698
+ min_pts = f"{row['min']:.0f}"
699
+ max_pts = f"{row['max']:.0f}"
700
+
701
+ output.append(f"Year {year:<10} {avg}")
702
+
703
+ output.append("=" * 90)
704
+ return "\n".join(output)
705
+
706
+
707
+
708
  # Load initial data
709
  print("πŸ“Š Loading initial data...")
710
  load_all_data()
 
853
  output.append("=" * 80)
854
  output.append("SYSTEM INFORMATION")
855
  output.append("=" * 80)
856
+
857
  # Average Points
858
  if 'average_points' in details_info:
859
  output.append("\nAVERAGE REWARD POINTS BY YEAR:")
 
861
  for year, points in details_info['average_points'].items():
862
  if points:
863
  output.append(f"Year {year:<10}: {points}")
864
+
865
+ # Calculated Year-wise Average Points
866
+ calculated_averages = calculate_yearwise_average_points()
867
+ if calculated_averages and not calculated_averages.startswith("❌") and not calculated_averages.startswith("⚠️"):
868
+ output.append(calculated_averages)
869
 
870
  # Redemption Dates
871
  if 'ip1_redemption' in details_info: