Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- # 1. Load the input file
- file_path = '/Users/divyamgoel/Desktop/ERP_MAIN/ERP/include/python_codes/old/BTP-ORDER.csv'
- #sample - https://www.x.com/scl/fi/0vqoeszcdfpw3623b9z9f/BTP-ORDER.csv?rlkey=afg5et38epsucg9nctoajv4uq&dl=0
- df = pd.read_csv(file_path)
- OUTPUT_FILE = str(file_path.split('.')[0])+"_allocation.csv"
- # Added the new output file path
- PREF_COUNT_FILE = str(file_path.split('.')[0])+"_pref_count.csv"
- print(OUTPUT_FILE)
- print(PREF_COUNT_FILE)
- # 2. Sort students by CGPA in decreasing order
- df_sorted = df.sort_values(by='CGPA', ascending=False, kind='stable').reset_index(drop=True)
- # 3. Identify professor preference columns
- prof_cols = [col for col in df.columns if 'Supervisor Preference [' in col]
- num_profs = len(prof_cols)
- allocation_results = []
- available_profs = set()
- # 4. Allocation Logic (Unchanged)
- for i, row in df_sorted.iterrows():
- if i % num_profs == 0:
- available_profs = set(prof_cols)
- student_prefs = row[prof_cols].sort_values().index.tolist()
- allotted_prof_col = None
- rank_of_allotment = None
- for prof_col in student_prefs:
- if prof_col in available_profs:
- allotted_prof_col = prof_col
- rank_of_allotment = row[prof_col]
- available_profs.remove(prof_col)
- break
- prof_name = allotted_prof_col.replace('Supervisor Preference [', '').replace(']', '')
- allocation_results.append({
- 'Roll Number': row['Roll Number'],
- 'Full Name': row['Full Name'],
- 'CGPA': row['CGPA'],
- 'Allotted Professor': prof_name,
- 'Preference Rank': int(rank_of_allotment)
- })
- # 5. Save the final allocation output to CSV
- output_df = pd.DataFrame(allocation_results)
- output_df.to_csv(OUTPUT_FILE, index=False)
- # --- NEW SECTION: Preference Count Logic ---
- # Calculate how many times each rank (1, 2, 3...) appears for each professor
- # We transpose it so professors are rows and ranks (1, 2, 3...) are columns
- pref_counts = df[prof_cols].apply(pd.Series.value_counts).fillna(0).astype(int).T
- # Clean the index (Professor names) to match your previous formatting
- pref_counts.index = [name.replace('Supervisor Preference [', '').replace(']', '') for name in pref_counts.index]
- pref_counts.index.name = 'Professor'
- # Save the preference counts to CSV
- pref_counts.to_csv(PREF_COUNT_FILE)
- # --- END NEW SECTION ---
- print(f"Allocation complete. Results saved to {OUTPUT_FILE}")
- print(f"Preference counts saved to {PREF_COUNT_FILE}")
Advertisement
Add Comment
Please, Sign In to add comment