-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathpython_data_science_toolbox_1.py
412 lines (278 loc) · 9.55 KB
/
python_data_science_toolbox_1.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import pandas as pd
import csv
from pprint import pprint as pp
from functools import reduce
def read_out_csv():
with open('tweets.csv') as fp:
reader = csv.reader(fp)
for row in reader:
print(row)
def make_df(file_name):
return pd.read_csv(file_name)
def square(value: float) -> float: # Function Header with parameters and type hints
"""
Return the square of a value
:param value: float
:return: float
"""
new_value = value ** 2
print(new_value) # Function Body
return new_value
in_num = 3.0
num = square(in_num)
print('\nOutput from the function square:')
print(f'The square of {in_num} is {num} and its type is {type(num)}\n')
def raise_to_power(value1: float, value2: float) -> tuple:
"""
Raise value1 to the power of value2 and vise versa
:param value1: float
:param value2: float
:return: tuple of floats
"""
new_value1 = value1 ** value2
new_value2 = value2 ** value1
return new_value1, new_value2
in_num = [10.0, 40.5]
num1, num2 = raise_to_power(in_num[0], in_num[1])
print('\nOutput from the function raise_to_power:')
print(f'Value1^Value2: {num1}\nValue2^Value1: {num2}\n')
def tweets():
# Import Twitter data as DataFrame: df
df = pd.read_csv('tweets.csv')
# Initialize an empty dictionary: langs_count
langs_count = {}
# Extract column from DataFrame: col
col = df['lang']
# Iterate over lang column in DataFrame
for entry in col:
# If the language is in langs_count, add 1
if entry in langs_count.keys():
langs_count[entry] = langs_count[entry] + 1
# Else add the language to langs_count, set the value to 1
else:
langs_count[entry] = 1
# Print the populated dictionary
print(langs_count)
print('\nOutput from the function tweets:')
tweets()
def count_entries(df, col_name):
"""Return a dictionary with counts of
occurrences as value for each key."""
# Initialize an empty dictionary: langs_count
langs_count = {}
# Extract column from DataFrame: col
col = df[col_name]
# Iterate over lang column in DataFrame
for entry in col:
# If the language is in langs_count, add 1
if entry in langs_count.keys():
langs_count[entry] = langs_count[entry] + 1
# Else add the language to langs_count, set the value to 1
else:
langs_count[entry] = 1
# Return the langs_count dictionary
return langs_count
# Call count_entries(): result
result = count_entries(make_df('tweets.csv'), 'lang')
# Print the result
print('\nOutput from the function count_entries:')
print(result)
def count_entries2(df, col_name='lang'):
"""Return a dictionary with counts of
occurrences as value for each key."""
# Initialize an empty dictionary: cols_count
cols_count = {}
# Extract column from DataFrame: col
col = df[col_name]
# Iterate over the column in DataFrame
for entry in col:
# If entry is in cols_count, add 1
if entry in cols_count.keys():
cols_count[entry] += 1
# Else add the entry to cols_count, set the value to 1
else:
cols_count[entry] = 1
# Return the cols_count dictionary
return cols_count
tweets_df = make_df('tweets.csv')
# Call count_entries(): result1
result1 = count_entries2(tweets_df)
# Call count_entries(): result2
result2 = count_entries2(tweets_df, col_name='source')
# Print result1 and result2
print('\nOutput from the function count_entries2:')
print('Result1:')
pp(result1)
print('Result2:')
pp(result2)
def count_entries3(df, *args):
"""Return a dictionary with counts of
occurrences as value for each key."""
# Initialize an empty dictionary: cols_count
cols_count = {}
# Iterate over column names in args
for col_name in args:
# Extract column from DataFrame: col
col = df[col_name]
# Iterate over the column in DataFrame
for entry in col:
# If entry is in cols_count, add 1
if entry in cols_count.keys():
cols_count[entry] += 1
# Else add the entry to cols_count, set the value to 1
else:
cols_count[entry] = 1
# Return the cols_count dictionary
return cols_count
# Call count_entries(): result1
result1 = count_entries3(tweets_df, 'lang')
# Call count_entries(): result2
result2 = count_entries3(tweets_df, 'lang', 'source')
# Print result1 and result2
print('\nOutput from the function count_entries3:')
print('Result1:')
pp(result1)
print('Result2:')
pp(result2)
print('\n')
# Lambda Functions
print('\n')
print('Lambda Functions: https://www.geeksforgeeks.org/python-lambda-anonymous-functions-filter-map-reduce/')
print('\n')
def ex_1():
nums = [48, 6, 9, 21, 1]
square_all = map(lambda num: num ** 2, nums)
print('Using Lambda Functions:')
print(square_all)
print(list(square_all))
print('\n')
ex_1()
def ex_2():
add_bangs = (lambda a: a + '!!!')
print(add_bangs('hello'))
ex_2()
def ex_3():
# Define echo_word as a lambda function: echo_word
echo_word = (lambda word1, echo: word1 * echo)
# Call echo_word: result
result_ex_3 = echo_word('hey', 5)
# Print result
print('\nResult ex_3:')
print(result_ex_3)
ex_3()
def ex_4():
# Create a list of strings: spells
spells = ["protego", "accio", "expecto patronum", "legilimens"]
# Use map() to apply a lambda function over spells: shout_spells
shout_spells = map(lambda x: x + '!!!', spells)
# Convert shout_spells to a list: shout_spells_list
shout_spells_list = list(shout_spells)
# Convert shout_spells into a list and print it
print('\nResult ex_4: shout_spells')
print(shout_spells_list)
ex_4()
def ex_5():
# Create a list of strings: fellowship
fellowship = ['frodo', 'samwise', 'merry', 'aragorn', 'legolas', 'boromir', 'gimli']
# Use filter() to apply a lambda function over fellowship: result
result_ex_5 = filter(lambda x: (len(x) > 6), fellowship)
# Convert result to a list: result_list
result_list = list(result_ex_5)
# Convert result into a list and print it
print('\nResult ex_5: fellowship')
print(result_list)
ex_5()
def ex_6():
# Import reduce from functools -> above
# Create a list of strings: stark
stark = ['robb', 'sansa', 'arya', 'eddard', 'jon']
# Use reduce() to apply a lambda function over stark: result
result_ex_6 = reduce((lambda item1, item2: (item1 + item2)), stark)
# Print the result
print('\nResult ex_6: starks')
print(result_ex_6)
ex_6()
# Error Handling
print('\nError Handling:\n')
def shout_echo(word1, echo=1):
"""Concatenate echo copies of word1 and three
exclamation marks at the end of the string."""
# Raise an error with raise
if echo < 0:
raise ValueError('echo must be greater than 0')
# Concatenate echo copies of word1 using *: echo_word
echo_word = word1 * echo
# Concatenate '!!!' to echo_word: shout_word
shout_word = echo_word + '!!!'
# Return shout_word
return shout_word
# Call shout_echo
print('\nResult shout_echo')
result_shout_echo = shout_echo("particle", echo=5)
print(result_shout_echo)
# Bring it all together
print('\nBring it all together!\n')
def ex_7(df):
# Select retweets from the Twitter DataFrame: result
result_ex_7 = filter(lambda x: x[0:2] == 'RT', df['text'])
# Create list from filter object result: res_list
res_list = list(result_ex_7)
# Print all retweets in res_list
for tweet in res_list:
print(tweet)
print('\nResults of ex_7')
ex_7(tweets_df)
def count_entries_4(df, col_name='lang'):
"""Return a dictionary with counts of
occurrences as value for each key."""
# Initialize an empty dictionary: cols_count
cols_count = {}
# Add try block
try:
# Extract column from DataFrame: col
col = df[col_name]
# Iterate over the column in dataframe
for entry in col:
# If entry is in cols_count, add 1
if entry in cols_count.keys():
cols_count[entry] += 1
# Else add the entry to cols_count, set the value to 1
else:
cols_count[entry] = 1
# Return the cols_count dictionary
return cols_count
# Add except block
except KeyError:
print('The DataFrame does not have a ' + col_name + ' column.')
print('\nResults of count_entries_4')
# Call count_entries(): result1
result1 = count_entries_4(tweets_df, 'lang')
# Print result1
print(result1)
# Call count_entries(): result2
result2 = count_entries_4(tweets_df, 'lang1')
print(result2)
def count_entries_5(df, col_name='lang'):
"""Return a dictionary with counts of
occurrences as value for each key."""
# Raise a ValueError if col_name is NOT in DataFrame
if col_name not in df.columns:
raise ValueError('The DataFrame does not have a ' + col_name + ' column.')
# Initialize an empty dictionary: cols_count
cols_count = {}
# Extract column from DataFrame: col
col = df[col_name]
# Iterate over the column in DataFrame
for entry in col:
# If entry is in cols_count, add 1
if entry in cols_count.keys():
cols_count[entry] += 1
# Else add the entry to cols_count, set the value to 1
else:
cols_count[entry] = 1
# Return the cols_count dictionary
return cols_count
print('\nResults of count_entries_5')
# Call count_entries(): result1
result1 = count_entries_5(tweets_df)
print(result1)