-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracExam.py
More file actions
65 lines (50 loc) · 1.83 KB
/
pracExam.py
File metadata and controls
65 lines (50 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Import necessary libraries
import pandas as pd
import re
# Load the data from the CSV file
df = pd.read_csv('ipes.csv')
# Convert column names to lowercase and replace non-alphanumeric characters with underscores
df.columns = [re.sub(r'[^a-zA-Z0-9_]', '_', col.lower()) for col in df.columns]
# Aggregate by 'faculty', 'item', and 'rating'
agg_data = df.groupby(['faculty', 'item', 'rating']).size().unstack(fill_value=0)
# Calculate mean for each item
agg_data['mean'] = agg_data.apply(lambda row: sum(row * row.index) / sum(row) if sum(row) > 0 else 0, axis=1)
# Display the first row
print('=' * 47)
print(f"{'Item':>15}{'1':>4}{'2':>4}{'3':>4}{'4':>4}{'5':>4}{'Mean':>5}{'Median':>7}")
print('-' * 47)
# Reset the index of agg_data
agg_data_reset = agg_data.reset_index()
# Initialize variables for current faculty
current_faculty = None
# Loop through aggregated data and print tables
for index, row in agg_data_reset.iterrows():
if row['faculty'] != current_faculty:
if current_faculty is not None:
print()
print(f"Instructor : {int(row['faculty'])}")
current_faculty = row['faculty']
# Caculating Median Algorithm
values = [int(row[col]) for col in range(1, 6)]
even = False
sum_values = sum(values)
if sum_values % 2 == 0:
even = True
n = sum_values // 2
else:
n = (sum_values // 2) + 1
k = 1
sum2 = 0
while k < 6:
sum2 = sum2 + int(row[k])
if sum2 < n:
k = k + 1
else:
if sum2 > n:
median = k
elif even:
median = (2 * k + 1) // 2
else:
median = k
break
print(f"{int(row['item']):>15} |{int(row[1]): >3}{int(row[2]): >3}{int(row[3]): >4}{int(row[4]): >4}{int(row[5]): >4}{row['mean']:>5.2f}{median:>6.2f}")