Python Data Science Toolbox Overview
Python Data Science Toolbox Overview
object1 contains "data+analysis+visualization", object2 contains 3, object # Define the function shout
3 contains "13".
def shout():
object1 contains "dataanalysisvisualization", object2 contains 3, object3 c """Print a string with three exclamation marks"""
ontains "111". # Concatenate the strings: shout_word
x is a float, y1 is an float, and y2 is a str. Complete the function header by adding the parameter name, word.
Assign the result of concatenating word with '!!!' to shout_word.
x is a float, y1 is a str, and y2 is a NoneType. Print the value of shout_word.
Call the shout() function, passing to it the string, 'congratulations'. return shout_word
# Pass 'congratulations' to shout: yell
# Define shout with the parameter, word
yell = shout('congratulations')
def shout(word):
# Print yell
"""Print a string with three exclamation marks"""
print(yell)
# Concatenate the strings: shout_word
shout_word = word + '!!!' Functions with multiple parameters
In the function body, concatenate the string in word with '!!!' and # Define shout with parameters word1 and word2
assign to shout_word.
Replace the print() statement with the appropriate return statement. def shout(word1, word2):
Call the shout() function, passing to it the string, 'congratulations', and """Concatenate strings with three exclamation marks"""
assigning the call to the variable, yell.
# Concatenate word1 with '!!!': shout1
To check if yell contains the value returned by shout(), print the value
of yell. shout1 = word1 + '!!!'
# Define shout with the parameter, word # Concatenate word2 with '!!!': shout2
def shout(word): shout2 = word2 + '!!!'
"""Return a string with three exclamation marks"""
# Concatenate the strings: shout_word # Concatenate shout1 with shout2: new_shout
shout_word = word + '!!!' new_shout = shout1 + shout2
func1() prints out 3, func2() prints out 10, and the value of num in
# Return the langs_count dictionary the global scope is 10.
return langs_count
func1() prints out 3, func2() prints out 10, and the value of num in
# Call count_entries(): result the global scope is 6.
result = count_entries(tweets_df, 'lang')
The keyword global
# Print the result
print(result) Use the keyword global to alter the object team in the global scope.
Change the value of team in the global scope to the string "justice
league". Assign the result to team.
Pop quiz on understanding scope
Hit the Submit button to see how executing your newly defined
def func1(): function change_team() changes the value of the name team!
num = 3
# Create a string: team
print(num)
team = "teen titans"
# Define change_team()
def func2():
def change_team():
global num
"""Change the value of the global variable team."""
double_num = num * 2
num = 6
# Use team in global scope
print(double_num)
global team
Complete the return value: each element of the tuple should be a call
to inner(), passing in the parameters from three_shouts() as arguments
# Change the value of team in global: team to each call.
team = "justice league"
# Print team # Define three_shouts
print(team) def three_shouts(word1, word2, word3):
# Call change_team() """Returns a tuple of strings
change_team() concatenated with '!!!'."""
# Print team
print(team) # Define inner
def inner(word):
Python’s built-in scope """Returns a string concatenated with '!!!'."""
Here you’re going to check out Python’s built-in scope, which is really just a return word + '!!!'
built-in module called builtins. However, to query builtins, you’ll need
to import builtins ‘because the name builtins is not itself built in…No, I’m
serious!’ (Learning Python, 5th edition, Mark Lutz). After executing import # Return a tuple of strings
builtins in the IPython Shell, execute dir(builtins) to print a list of all the
names in the module builtins. Have a look and you’ll see a bunch of names return (inner(word1), inner(word2), inner(word3))
that you’ll recognize! Which of the following names is NOT in the module # Call three_shouts() and print
builtins?
print(three_shouts('a', 'b', 'c'))
Assign to echo_word the string word, concatenated with itself. # Print echo_word
Use the keyword nonlocal to alter the value of echo_word in the
print(echo_word)
enclosing scope.
Alter echo_word to echo_word concatenated with ‘!!!’. # Call function echo_shout() with argument 'hello'
Call the function echo_shout(), passing it a single argument ‘hello’. echo_shout('hello')
# Concatenate '!!!' to echo_word: shout_word # Concatenate echo copies of word1 using *: echo_word
shout_word = echo_word + '!!!' echo_word = word1 * echo
How would you write a lambda function add_bangs that adds three
# Extract column from DataFrame: col exclamation points '!!!' to the end of a string a?
col = df[col_name] How would you call add_bangs with the argument 'hello'?
if entry in cols_count.keys():
The lambda function definition is: (lambda a: a + '!!!') = add_bangs, and
cols_count[entry] += 1 the function call is: add_bangs('hello').
Map() and lambda functions fellowship = ['frodo', 'samwise', 'merry', 'pippin', 'aragorn', 'boromir', 'legolas',
'gimli', 'gandalf']
For example:
# Use filter() to apply a lambda function over fellowship: result
nums = [2, 4, 6, 8, 10] result = filter(lambda member: len(member) > 6, fellowship)
# Convert result to a list: result_list
result = map(lambda a: a ** 2, nums) result_list = list(result)
# Print result_list
In the map() call, pass a lambda function that concatenates the
string '!!!' to a string item; also pass the list of strings, spells. Assign print(result_list)
the resulting map object to shout_spells.
Convert shout_spells to a list and print out the list. Reduce() and lambda functions
Remember gibberish() from a few exercises back?
# Create a list of strings: spells
# Define gibberish
def gibberish(*args): len(525600)
"""Concatenate strings in *args together."""
hodgepodge = '' len(('jaime', 'cersei', 'tywin', 'tyrion', 'joffrey'))
for word in args: Which of the function calls raises an error and what type of error is raised?
hodgepodge += word
return hodgepodge The call len('There is a beast in every man and it stirs when you put a
sword in his hand.') raises a TypeError.
Import the reduce function from the functools module.
In the reduce() call, pass a lambda function that takes two string The call len(['robb', 'sansa', 'arya', 'eddard', 'jon']) raises an IndexError.
arguments item1 and item2 and concatenates them; also pass the list
of strings, stark. Assign the result to result. The first argument
The call len(525600) raises a TypeError.
to reduce() should be the lambda function and the second argument is
the list stark.
The call len(('jaime', 'cersei', 'tywin', 'tyrion', 'joffrey')) raises
a NameError.
# Import reduce from functoolsfrom functools import reduce
# Create a list of strings: stark Error handling with try-except
stark = ['robb', 'sansa', 'arya', 'brandon', 'rickon']
Initialize the variables echo_word and shout_words to empty strings.
# Use reduce() to apply a lambda function over stark: result Add the keywords try and except in the appropriate locations for the
result = reduce(lambda item1, item2: item1 + item2, stark) exception handling block.
Use the * operator to concatenate echo copies of word1. Assign the
# Print the result result to echo_word.
print(result) Concatenate the string '!!!' to echo_word. Assign the result
to shout_words.
Pop quiz about errors
Take a look at the following function calls to len(): # Define shout_echodef shout_echo(word1, echo=1):
"""Concatenate echo copies of word1 and three
len('There is a beast in every man and it stirs when you put a sword in his han
d.') exclamation marks at the end of the string."""
len(['robb', 'sansa', 'arya', 'eddard', 'jon']) # Initialize empty strings: echo_word, shout_words
echo_word = ''
shout_words = ''
# Raise an error with raise
# Add exception handling with try-except if echo < 0:
try: raise ValueError('echo must be greater than or equal to 0')
# Concatenate echo copies of word1 using *: echo_word
echo_word = word1 * echo # Concatenate echo copies of word1 using *: echo_word
echo_word = word1 * echo
# Concatenate '!!!' to echo_word: shout_words
shout_words = echo_word + '!!!' # Concatenate '!!!' to echo_word: shout_word
except: shout_word = echo_word + '!!!'
# Print error message
print("word1 must be a string and echo must be an integer.") # Return shout_word
return shout_word
# Return shout_words # Call shout_echo
return shout_words shout_echo("particle", echo=5)
# Call shout_echo
Bringing it all together
shout_echo("particle", echo="accelerator")
Bringing it all together (1)
Error handling by raising an error
In the filter() call, pass a lambda function and the sequence of tweets
Complete the if statement by checking if the value of echo is less as strings, tweets_df['text']. The lambda function should check if the
than 0. first 2 characters in a tweet x are ‘RT’. Assign the resulting filter
In the body of the if statement, add a raise statement that raises object to result. To get the first 2 characters in a tweet x, use x[0:2].
a ValueError with message 'echo must be greater than or equal to To check equality, use a Boolean filter with ==.
0' when the value supplied by the user to echo is less than 0. Convert result to a list and print out the list.
# Define shout_echodef shout_echo(word1, echo=1): # Select retweets from the Twitter DataFrame: result
"""Concatenate echo copies of word1 and three result = filter(lambda x: x[0:2] == 'RT', tweets_df['text'])
exclamation marks at the end of the string.""" # Create list from filter object result: res_list
res_list = list(result) if entry in cols_count.keys():
# Print all retweets in res_listfor tweet in res_list: cols_count[entry] += 1
print(tweet) # Else add the entry to cols_count, set the value to 1
else:
Bringing it all together (2)
cols_count[entry] = 1
Add a try block so that when the function is called with the correct
arguments, it processes the DataFrame and returns a dictionary of
# Return the cols_count dictionary
results.
Add an except block so that when the function is called incorrectly, it return cols_count
displays the following error message: 'The DataFrame does not have a
' + col_name + ' column.'.
# Add except block
# Define count_entries()def count_entries(df, col_name='lang'): except:
"""Return a dictionary with counts of print('The DataFrame does not have a ' + col_name + ' column.')
occurrences as value for each key.""" # Call count_entries(): result1
result1 = count_entries(tweets_df, 'lang')
# Initialize an empty dictionary: cols_count # Print result1
cols_count = {} print(result1)