-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
75 lines (52 loc) · 1.2 KB
/
analysis.py
File metadata and controls
75 lines (52 loc) · 1.2 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
66
import pyodbc
#import pandas as pd
# Connect to SQL Server
conn = pyodbc.connect(
'DRIVER={SQL Server};'
'SERVER=ARYA-SINGH\\SQLEXPRESS;'
'DATABASE=StudentDB;'
'Trusted_Connection=yes;'
)
print("connected successfully")
#ANALYSIS
import pandas as pd
import matplotlib.pyplot as plt
#query = "SELECT * FROM Students"
#df = pd.read_sql(query, conn)# fetch data
#print(df)
#join query
query = """
SELECT s.name, m.subject, m.marks
FROM Students s
JOIN Marks m ON s.student_id = m.student_id
"""
df = pd.read_sql(query, conn)
print(df)
print(df.columns)
#AVERAGE MARKS
avg = df.groupby('name')['marks'].mean()
print("AVERAGE MARKS\n",avg)
#TOPPER
print("\nTopper ",avg.idxmax())
#subject average
sub_avg = df.groupby('subject')['marks'].mean()
print("Subject average\n",sub_avg)
# low marks
LOW = df[df['marks']<60]
print("LOW MARKS",LOW)
#save as csv
df.to_csv("librarystudent_data.csv",index=False)
# Graph
#graph1
avg.plot(kind='bar')
plt.title("Average Marks per Student")
plt.xlabel("Student")
plt.ylabel("Marks")
plt.show()
#graph2
sub_avg.plot(kind='bar')
plt.title("Subject-wise Average")
plt.xlabel("Subject")
plt.ylabel("Marks")
plt.show()#display the graph on screen.
#conn.close()