-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCODE.py
More file actions
144 lines (122 loc) · 3.46 KB
/
CODE.py
File metadata and controls
144 lines (122 loc) · 3.46 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from multiprocessing.sharedctypes import Value
from optparse import Values
from tkinter import font
from turtle import fillcolor
from click import style
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import csv
from fpdf import FPDF
from PIL import Image
p = "data.csv"
data = pd.read_csv(p)
def fn(data):
li = []
di = {}
for x in data.columns:
df = data[x]
if df.dtype == "O":
di[x] = fn1(df)
else:
li.append(x)
di[x] = fn2(df)
for i in range(len(li)):
for j in range(i + 1, len(li)):
di[li[i] + "-" + li[j]] = fn3(data[li[i]], data[li[j]], li[i], li[j])
return di
def fn1(df):
di = {}
vc = df.value_counts()
di["Value-count"] = ""
for i in range(len(vc)):
di["Value-count"] += str(vc.index[i]) + " " + str(vc[i]) + "\n"
img = plt.hist(df)
plt.title("HIST")
plt.savefig("img1.png")
img = plt.imread("./img1.png")
di["img1"] = img
plt.clf()
return di
def fn2(df):
di = {}
di["Mean"] = df.mean()
di["Median"] = df.median()
di["Range"] = str(df.min()) + " - " + str(df.max())
img = plt.hist(df)
plt.title("HIST")
plt.savefig("img2.png")
img = plt.imread("./img2.png")
di["img2"] = img
plt.clf()
return di
def fn3(df1, df2, x, y):
di = {}
img = plt.scatter(df1, df2)
plt.title("PLOT")
plt.xlabel(x)
plt.ylabel
plt.savefig("img3.png")
img = plt.imread("./img3.png")
di["img3"] = img
plt.clf()
return di
res = fn(data)
res1 = res.values()
# res1 = pd.DataFrame(res)
with open("data.csv", newline="") as f:
reader = csv.reader(f)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", "B", 20.0)
pdf.cell(200, 10, "TASK 2", align="C")
pdf.ln(10)
pdf.set_font("Courier", "", 12)
pdf.ln(1)
th = pdf.font_size
for i in res:
pdf.set_author("Teccrown Software")
pdf.set_font("Times", "I", 22.0)
# print(res[i])
pdf.cell(200, 10, i, align="C")
for col_name in res[i]:
pdf.set_font("Times", "B", 12.0)
pdf.ln(10)
if col_name == "img1":
pdf.image("img1.png", w=90, h=70, x=60)
if col_name == "img2" and i == "AGE":
pdf.image("img2.png", w=90, h=70, x=60)
if col_name == "img3":
pdf.image("img3.png", w=90, h=70, x=60)
continue
if col_name == "Value-count":
pdf.multi_cell(
200,
6,
"{}\n \n{}".format(col_name, res[i][col_name]),
align="L",
)
if col_name == "Mean":
pdf.multi_cell(
200,
2,
"{}: {}".format(col_name, res[i][col_name]),
align="L",
)
if col_name == "Median":
pdf.multi_cell(
200,
2,
"{}: {}".format(col_name, res[i][col_name]),
align="L",
)
if col_name == "Range":
pdf.multi_cell(
200,
2,
"{}: {}".format(col_name, res[i][col_name]),
align="L",
)
pdf.ln(10)
pdf.cell(200, 0.0, "-------------end of report----------", align="C")
pdf.output("student.pdf", "F")