Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import matplotlib.pyplot as plt
- def menu():
- print('+--------- MENU ---------+')
- print('| 1. Import excel file |')
- print('| 2. Print all data |')
- print('| 3. Calculation |')
- print('| 4. Stats |')
- print('| 5. Update data |')
- print('| 6. Export excel file |')
- print('| 7. Print menu |')
- print('| 8. Exit |')
- print('+------------------------+')
- def import_file():
- while True:
- try:
- global data
- file_path = input('Enter path: ')
- # print in row
- pd.set_option('expand_frame_repr', False)
- data = pd.read_excel(file_path)
- pd.options.display.max_rows = 100
- pd.options.display.max_columns = 20
- print('Success\n')
- break
- except IOError:
- print ("Error: can\'t find file or read data")
- def print_data():
- print('\n', data.to_string(index=False), '\n')
- def calculation(score):
- data['Quiz'] = round((data['Quiz 1'] + data['Quiz 2'] + data['Quiz 3']) / 3 + data['Bonus'] - data['Minus'], 1)
- data['GPA'] = round(score[0] * data['Quiz'] + score[1] * data['Midterm'] + score[2] * data['Final'], 1)
- i = 0
- for x in data['GPA']:
- if x >= 90:
- data.loc[i, 'Rank'] = 'A+'
- elif x >= 80:
- data.loc[i, 'Rank'] = 'A'
- elif x >= 70:
- data.loc[i, 'Rank'] = 'B+'
- elif x >= 60:
- data.loc[i, 'Rank'] = 'B'
- elif x >= 50:
- data.loc[i, 'Rank'] = 'C+'
- else:
- data.loc[i, 'Rank'] = 'F'
- if x >= 50:
- data.loc[i, 'Status'] = 'Pass'
- else:
- data.loc[i, 'Status'] = 'Fail'
- i += 1
- print('Success\n')
- def stats():
- mean = round(data['GPA'].mean(), 1)
- print('\n* Mean GPA =', mean)
- std = round(data['GPA'].std(), 1)
- print('* Stdev GPA =', std)
- min_index = data['GPA'].idxmin()
- print('* Min GPA:\n', data.loc[[min_index]].to_string(index=False))
- max_index = data['GPA'].idxmax()
- print('* Max GPA:\n', data.loc[[max_index]].to_string(index=False))
- c_pass = len(data[data['Status'] == 'Pass'])
- print('* Num pass =', c_pass)
- c_fail = len(data[data['Status'] == 'Fail'])
- print('* Num fail =', c_fail, '\n')
- # Bar Chart
- # Data
- r = [0, 2, 4, 6, 8, 10]
- c_quiz1_50 = len(data[data['Quiz 1'] < 50])
- c_quiz2_50 = len(data[data['Quiz 2'] < 50])
- c_quiz3_50 = len(data[data['Quiz 3'] < 50])
- c_quiz_50 = len(data[data['Quiz'] < 50])
- c_mid_50 = len(data[data['Midterm'] < 50])
- c_final_50 = len(data[data['Final'] < 50])
- c_quiz1_50_80 = len(data[data['Quiz 1'] < 80]) - c_quiz1_50
- c_quiz2_50_80 = len(data[data['Quiz 2'] < 80]) - c_quiz2_50
- c_quiz3_50_80 = len(data[data['Quiz 3'] < 80]) - c_quiz3_50
- c_quiz_50_80 = len(data[data['Quiz'] < 80]) - c_quiz_50
- c_mid_50_80 = len(data[data['Midterm'] < 80]) - c_mid_50
- c_final_50_80 = len(data[data['Final'] < 80]) - c_final_50
- c_quiz1_80 = len(data[data['Quiz 1'] >= 80])
- c_quiz2_80 = len(data[data['Quiz 2'] >= 80])
- c_quiz3_80 = len(data[data['Quiz 3'] >= 80])
- c_quiz_80 = len(data[data['Quiz'] >= 80])
- c_mid_80 = len(data[data['Midterm'] >= 80])
- c_final_80 = len(data[data['Final'] >= 80])
- raw_data = {'greenBars': [c_quiz1_50, c_quiz2_50, c_quiz3_50, c_quiz_50, c_mid_50, c_final_50],
- 'orangeBars': [c_quiz1_50_80, c_quiz2_50_80, c_quiz3_50_80, c_quiz_50_80, c_mid_50_80, c_final_50_80],
- 'blueBars': [c_quiz1_80, c_quiz2_80, c_quiz3_80, c_quiz_80, c_mid_80, c_final_80]}
- df = pd.DataFrame(raw_data)
- # From raw value to percentage
- totals = [i + j + k for i, j, k in zip(df['greenBars'], df['orangeBars'], df['blueBars'])]
- greenBars = [i / j * 100 for i, j in zip(df['greenBars'], totals)]
- orangeBars = [i / j * 100 for i, j in zip(df['orangeBars'], totals)]
- blueBars = [i / j * 100 for i, j in zip(df['blueBars'], totals)]
- # plot
- barWidth = 0.85
- names = ('Quiz 1', 'Quiz 2', 'Quiz 3', 'Quiz', 'Midterm', 'Final')
- # Create green Bars
- p1 = plt.bar(r, greenBars, color='#b5ffb9', edgecolor='white', width=barWidth)
- # Create orange Bars
- p2 = plt.bar(r, orangeBars, bottom=greenBars, color='#f9bc86', edgecolor='white', width=barWidth)
- # Create blue Bars
- p3 = plt.bar(r, blueBars, bottom=[i + j for i, j in zip(greenBars, orangeBars)], color='#a3acff', edgecolor='white',
- width=barWidth)
- # Custom x axis
- plt.xticks(r, names)
- plt.xlabel("Scores")
- plt.legend((p1[0], p2[0], p3[0]), ('score < 50', '50 <= score < 80 ', 'score >= 80'))
- # Box Plot
- plt.subplots(figsize=(5, 5), dpi=88)
- plt.title('Box Plot')
- plt.ylabel('GPA')
- plt.boxplot(data['GPA'])
- # Pie Chart
- labels = ['F', 'C+', 'B', 'B+', 'A', 'A+']
- values = [len(data[data['Rank'] == 'F']), len(data[data['Rank'] == 'C+']), len(data[data['Rank'] == 'B']),
- len(data[data['Rank'] == 'B+']), len(data[data['Rank'] == 'A']), len(data[data['Rank'] == 'A+'])]
- fig1, ax1 = plt.subplots(figsize=(5, 5), dpi=88)
- plt.title('Pie Chart')
- ax1.pie(values, labels=labels, autopct='%1.1f%%', startangle=90)
- plt.legend(labels=labels)
- plt.show()
- def bar_label(ps):
- for p in ps:
- plt.text(p.get_x() + p.get_width() / 2., p.get_y() + p.get_height() / 2.,
- '%.1f' % p.get_height() + '%', ha='center', va='center')
- def update_data():
- global score
- while True:
- print()
- # Choose student
- while True:
- i = eval(input('Input No.: '))
- if i > len(data):
- print('No. out of range.')
- else:
- print('\n', data.loc[[i - 1]].to_string(index=False))
- break
- # Check whether that is the needed student
- re_print = 0
- while True:
- y = input('Do you want to continue to change the data of this student? (Y/N): ')
- if y != 'Y'and y != 'N':
- print('Wrong input.')
- elif y == 'Y':
- list_col = ['Quiz 1', 'Quiz 2', 'Quiz 3', 'Bonus', 'Minus']
- while True:
- col_name = input('Input Col name: ')
- if col_name in list_col:
- break
- else:
- print('The change can only be made for Quiz 1, Quiz 2, Quiz 3, Bonus, Minus.')
- while True:
- new_value = eval(input('Input new value: '))
- if new_value < 0 or new_value > 100:
- print("Wrong input.")
- else:
- break
- data.loc[i - 1, col_name] = new_value
- re_print = 1
- if len(score) == 3:
- calculation(score)
- else:
- if re_print == 1:
- print('\n', data.loc[[i - 1]].to_string(index=False))
- break
- # Continue to chang data
- while True:
- k = input('Do you want to change anything else? (Y/N): ')
- if k != 'Y' and k != 'N':
- print('Wrong input.')
- else:
- break
- if k == 'N':
- print()
- break
- def export_file():
- while True:
- try:
- file_path = input('Enter export path: ')
- data.to_excel(file_path, index=False)
- print('Success\n')
- break
- except ValueError:
- print("Error: invalid input.")
- data = ''
- menu()
- print("Inport excel file")
- import_file()
- score = ''
- while True:
- try:
- n = eval(input('Input a number: '))
- if n == 1:
- import_file()
- elif n == 2:
- print_data()
- elif n == 3:
- while True:
- try:
- score = [float(x) for x in input('Input % [Quiz, Midterm, Final]: ').split(',')]
- if score[0] + score[1] + score[2] != 1:
- print("Wrong input.")
- else:
- calculation(score)
- print_data()
- break
- except ValueError:
- print("Error: invalid input.")
- except IndexError:
- print("Error: invalid input.")
- elif n == 4:
- if len(score) != 3:
- print("Calculation must be done before Stats.")
- else:
- stats()
- elif n == 5:
- update_data()
- elif n == 6:
- export_file()
- elif n == 7:
- menu()
- elif n == 8:
- exit()
- else:
- print('Please enter a number between 1 and 8!')
- except NameError:
- print ("Error: invalid input.")
- except SyntaxError:
- print ("Error: invalid input.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement