-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data_visualisations.py
executable file
·85 lines (73 loc) · 2.95 KB
/
Data_visualisations.py
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
from pymongo.mongo_client import MongoClient
import matplotlib.pyplot as plt
import pandas as pd
class Viz:
try:
myclient = MongoClient(host="mongodb+srv://cluster0-jxbdj.mongodb.net/test",
username='haseeb',
password='123')
db = myclient['case_study']
print('connection to MongoDB was successful')
except Exception:
print("did not connect")
def __init__(self):
self.myclient = Viz.myclient
self.db = Viz.db
def A(self):
try:
self.col = self.db.ServiceArea
pandf = pd.DataFrame(list(self.col.find()))
state_count_df = pandf.groupby('StateCode').count()
a = state_count_df[['ServiceAreaName', 'SourceName', 'BusinessYear']]
a.plot.bar(figsize=(10, 10))
plt.show()
except Exception:
print('''it looks like this data hasn't been loaded in Mongo Yet.
Please make sure that isn't the case''')
def B(self):
# The following plot shows the sourcename for the country.
try:
self.col = self.db["ServiceArea"]
df = self.col.groupby('SourceName').count()
sc = df[['SourceName', 'County']]
sc.plot.bar(figsize=(10, 10))
plt.show()
except Exception:
print('''it looks like this data hasn't been loaded in Mongo Yet.
Please make sure that isn't the case''')
def D(self):
try:
self.col = self.db.BenefitsCostSharing
pandf = pd.DataFrame(list(self.col.find()))
b = pandf[['BenefitName', 'StateCode']].groupby(['StateCode'])['BenefitName'].count()
b.plot.bar(figsize=(10, 10))
plt.show()
except Exception:
print('''it looks like this data hasn't been loaded in Mongo Yet.
Please make sure that isn't the case''')
def E(self):
try:
self.col = self.db.insurance
data = pd.DataFrame(list(self.col.find()))
abc = data.groupby('region').count()['_id']
sm = data[data.smoker == 'yes'].groupby('region').count()['_id']
xyz = sm / abc
xyz.plot.bar(figsize=(10, 10))
plt.show()
except Exception:
print('''it looks like this data hasn't been loaded in Mongo Yet.
Please make sure that isn't the case''')
def F(self):
'''Question D -- Benefit plans per state'''
self.col = self.db.insurance
pdf = pd.DataFrame(list(self.col.find()))
abc = pdf.groupby('region').count()['_id']
sm = pdf[pdf.smoker == 'yes'].groupby('region').count()['_id']
xyz = sm / abc
xyz.plot.bar(figsize=(10, 10))
plt.show()
def main():
V = Viz()
V.F()
if __name__ == '__main__':
main()