-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionaries and Frequency Tables-314.py
179 lines (122 loc) · 3.79 KB
/
Dictionaries and Frequency Tables-314.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
## 1. Storing Data ##
content_ratings = ['4+', '9+', '12+', '17+']
numbers = [4433, 987, 1155, 622]
content_rating_numbers = [content_ratings, numbers]
## 2. Dictionaries ##
content_ratings = {'4+': 4433, '9+': 987, '12+': 1155, '17+': 622}
print (content_ratings)
## 3. Indexing ##
content_ratings = {'4+': 4433, '9+': 987, '12+': 1155, '17+': 622}
over_9 = content_ratings['9+']
over_17 = content_ratings['17+']
print(over_9)
print(over_17)
## 4. Alternative Method of Creating a Dictionary ##
content_ratings = {}
content_ratings['12+'] = 1155
content_ratings['17+'] = 622
content_ratings['4+'] = 4433
content_ratings['9+'] = 987
print(content_ratings)
over_12_n_apps = content_ratings['12+']
print(over_12_n_apps)
## 5. Key-Value Pairs ##
d_1 = {'key_1': 'first_value',
'key_2': 2,
'key_3': 3.14,
'key_4': True,
'key_5': [4,2,1],
'key_6': {'inner_key' : 6}
}
print(d_1)
error = True
## 6. Checking for Membership ##
content_ratings = {'4+': 4433, '9+': 987, '12+': 1155, '17+': 622}
is_in_dictionary_1 = '9+' in content_ratings
is_in_dictionary_2 = 987 in content_ratings
if '17+' in content_ratings:
result = "It exists"
print (result)
## 7. Counting with Dictionaries ##
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
header = apps_data[0]
apps_data = apps_data[1:]
content_ratings = {'4+': 0, '9+': 0, '12+': 0,'17+': 0}
for row in apps_data:
c_rating = row[10]
if c_rating in content_ratings:
content_ratings[c_rating] += 1
print(content_ratings)
## 8. Finding the Unique Values ##
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
header = apps_data[0]
apps_data = apps_data[1:]
content_ratings ={}
for row in apps_data:
c_rating= row[10]
if c_rating in content_ratings:
content_ratings[c_rating]+= 1
else:
content_ratings[c_rating] =1
print(content_ratings)
## 9. Proportions and Percentages ##
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
header = apps_data[0]
apps_data = apps_data[1:]
genre_counting = {}
for row in apps_data:
genre = row[11]
if genre in genre_counting:
genre_counting[genre] += 1
else:
genre_counting[genre] =1
print (genre_counting)
## 10. Looping over Dictionaries ##
content_ratings = {'4+': 4433, '12+': 1155, '9+': 987, '17+': 622}
total_number_of_apps = 7197
for interaction_variable in content_ratings:
content_ratings[interaction_variable] /= total_number_of_apps
content_ratings[interaction_variable] *=100
percentage_17_plus = content_ratings['17+']
percentage_15_allowed = content_ratings['4+'] + content_ratings['9+'] + content_ratings['12+']
print (content_ratings)
print (percentage_15_allowed)
print (percentage_17_plus)
## 11. Keeping the Dictionaries Separate ##
content_ratings = {'4+': 4433, '12+': 1155, '9+': 987, '17+': 622}
total_number_of_apps = 7197
c_ratings_proportions = {}
c_ratings_percentages = {}
for key in content_ratings:
proportion = content_ratings[key] / total_number_of_apps
percentage = proportion *100
c_ratings_proportions[key] = proportion
c_ratings_percentages[key] = percentage
print (c_ratings_percentages)
print (c_ratings_proportions)
print (content_ratings)
## 12. Frequency Tables for Numerical Columns ##
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
header = apps_data[0]
apps_data = apps_data[1:]
data_sizes = []
for row in apps_data:
size = float(row [2])
data_sizes.append(size)
print (data_sizes)
min_size = min(data_sizes)
max_size = max (data_sizes)
print(min_size)
print(max_size)