0% found this document useful (0 votes)
37 views17 pages

Python Data Science Toolbox

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views17 pages

Python Data Science Toolbox

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

PYTHON DATA SCIENCE TOOLBOX (PART 1)

Strings in Python x is a float, y1 is an float, and y2 is a str.

Execute the following code in the shell:

object1 = "data" + "analysis" + "visualization" x is a float, y1 is a str, and y2 is a NoneType.


object2 = 1 * 3
object3 = "1" * 3 They are all NoneType types.
What are the values in object1, object2, and object3, respectively?
Write a simple function

def square():
object1 contains "data + analysis +
visualization", object2 contains "1*3", object3 contains 13. new_value = 4 ** 2
return new_value

object1 contains "data+analysis+visualization", object2 contains 3, object  Complete the function header by adding the appropriate function
3 contains "13". name, shout.
 In the function body, concatenate the string, 'congratulations' with
another string, '!!!'. Assign the result to shout_word.
object1 contains "dataanalysisvisualization", object2 contains 3, object3 c  Print the value of shout_word.
ontains "111".  Call the shout function.

Recapping built-in functions # Define the function shout

 Assign str(x) to a variable y1: y1 = str(x) def shout():


 Assign print(x) to a variable y2: y2 = print(x) """Print a string with three exclamation marks"""
 Check the types of the variables x, y1, and y2.
# Concatenate the strings: shout_word
What are the types of x, y1, and y2? shout_word = 'congratulations' + '!!!'

They are all str types. # Print shout_word


print(shout_word)
# Call shout # Define shout with the parameter, word
shout() def shout(word):
"""Return a string with three exclamation marks"""
Single-parameter functions
# Concatenate the strings: shout_word
 Complete the function header by adding the parameter name, word. shout_word = word + '!!!'
 Assign the result of concatenating word with '!!!' to shout_word.
 Print the value of shout_word.
 Call the shout() function, passing to it the string, 'congratulations'. # Replace print with return
return shout_word
# Define shout with the parameter, word # Pass 'congratulations' to shout: yell
def shout(word): yell = shout('congratulations')
"""Print a string with three exclamation marks""" # Print yell
# Concatenate the strings: shout_word print(yell)
shout_word = word + '!!!'
Functions with multiple parameters
# Print shout_word  Modify the function header such that it accepts two
print(shout_word) parameters, word1 and word2, in that order.
 Concatenate each of word1 and word2 with '!!!' and assign
# Call shout with the string 'congratulations'
to shout1 and shout2, respectively.
shout('congratulations')  Concatenate shout1 and shout2 together, in that order, and assign
to new_shout.
Functions that return single values  Pass the strings 'congratulations' and 'you', in that order, to a call
to shout(). Assign the return value to yell.
 In the function body, concatenate the string in word with '!!!' and
assign to shout_word. # Define shout with parameters word1 and word2
 Replace the print() statement with the appropriate return statement.
 Call the shout() function, passing to it the string, 'congratulations', and def shout(word1, word2):
assigning the call to the variable, yell. """Concatenate strings with three exclamation marks"""
 To check if yell contains the value returned by shout(), print the value
# Concatenate word1 with '!!!': shout1
of yell.
shout1 = word1 + '!!!'
# Construct even_nums
# Concatenate word2 with '!!!': shout2 even_nums = (2, num2, num3)
shout2 = word2 + '!!!'
Functions that return multiple values

# Concatenate shout1 with shout2: new_shout  Modify the function header such that the function name is
now shout_all, and it accepts two parameters, word1 and word2, in
new_shout = shout1 + shout2
that order.
 Concatenate the string '!!!' to each of word1 and word2 and assign
# Return new_shout to shout1 and shout2, respectively.
 Construct a tuple shout_words, composed of shout1 and shout2.
return new_shout  Call shout_all() with the strings 'congratulations' and 'you' and assign
# Pass 'congratulations' and 'you' to shout: yell the result to yell1 and yell2 (remember, shout_all() returns 2
variables!).
yell = shout('congratulations', 'you')
# Print yell # Define shout_all with parameters word1 and word2
print(yell) def shout_all(word1, word2):
"""Return a tuple of strings"""
A brief introduction to tuples
# Concatenate word1 with '!!!': shout1
 Print out the value of nums in the IPython shell. Note the elements in shout1 = word1 + '!!!'
the tuple.
 In the IPython shell, try to change the first element of nums to the
value 2 by doing an assignment: nums[0] = 2. What happens? # Concatenate word2 with '!!!': shout2

 Unpack nums to the variables num1, num2, and num3. shout2 = word2 + '!!!'
 Construct a new tuple, even_nums composed of the same elements
in nums, but with the 1st element replaced with the value, 2.
# Construct a tuple with shout1 and shout2: shout_words
shout_words = (shout1, shout2)
# edited/added
nums = (3,4,6)
# Return shout_words
# Unpack nums into num1, num2, and num3
return shout_words
num1, num2, num3 = nums
# Pass 'congratulations' and 'you' to shout_all(): yell1, yell2
yell1, yell2 = shout_all('congratulations', 'you') # If the language is in langs_count, add 1
# Print yell1 and yell2 if entry in langs_count.keys():
print(yell1) langs_count[entry] += 1
print(yell2) # Else add the language to langs_count, set the value to 1
else:
Bringing it all together (1)
langs_count[entry] = 1
 Import the pandas package with the alias pd. # Print the populated dictionary
 Import the file 'tweets.csv' using the pandas function read_csv().
print(langs_count)
Assign the resulting DataFrame to df.
 Complete the for loop by iterating over col, the 'lang' column in the
DataFrame df. Bringing it all together (2)
 Complete the bodies of the if-else statements in the for loop: if the
key is in the dictionary langs_count, add 1 to the value corresponding  Define the function count_entries(), which has two parameters. The
to this key in the dictionary, else add the key to langs_count and set first parameter is df for the DataFrame and the second is col_name for
the corresponding value to 1. Use the loop variable entry in your the column name.
code.  Complete the bodies of the if-else statements in the for loop: if the
key is in the dictionary langs_count, add 1 to its current
value, else add the key to langs_count and set its value to 1. Use the
# Import pandas loop variable entry in your code.
import pandas as pd  Return the langs_count dictionary from inside
the count_entries() function.
# Import Twitter data as DataFrame: df
 Call the count_entries() function by passing to it tweets_df and the
df = pd.read_csv('tweets.csv') name of the column, 'lang'. Assign the result of the call to the
# Initialize an empty dictionary: langs_count variable result.

langs_count = {}
# edited/added
# Extract column from DataFrame: col
tweets_df = pd.read_csv('tweets.csv')
col = df['lang']
# Iterate over lang column in DataFrame
# Define count_entries()
for entry in col:
def count_entries(df, col_name):
"""Return a dictionary with counts of Pop quiz on understanding scope

occurrences as value for each key.""" def func1():


num = 3
# Initialize an empty dictionary: langs_count print(num)
langs_count = {}
def func2():
# Extract column from DataFrame: col global num
col = df[col_name] double_num = num * 2
num = 6
# Iterate over lang column in DataFrame print(double_num)
for entry in col: Try calling func1() and func2() in the shell, then answer the following
questions:
# If the language is in langs_count, add 1 What are the values printed out when you call func1() and func2()?
if entry in langs_count.keys(): What is the value of num in the global scope after
calling func1() and func2()?
langs_count[entry] += 1
# Else add the language to langs_count, set the value to 1 func1() prints out 3, func2() prints out 6, and the value of num in
else: the global scope is 3.
langs_count[entry] = 1
func1() prints out 3, func2() prints out 3, and the value of num in
the global scope is 3.
# Return the langs_count dictionary
return langs_count func1() prints out 3, func2() prints out 10, and the value of num in
the global scope is 10.
# Call count_entries(): result
result = count_entries(tweets_df, 'lang') func1() prints out 3, func2() prints out 10, and the value of num in
# Print the result the global scope is 6.
print(result) The keyword global
 Use the keyword global to alter the object team in the global scope. builtins in the IPython Shell, execute dir(builtins) to print a list of all the
 Change the value of team in the global scope to the string "justice names in the module builtins. Have a look and you’ll see a bunch of names
league". Assign the result to team. that you’ll recognize! Which of the following names is NOT in the module
 Hit the Submit button to see how executing your newly defined builtins?
function change_team() changes the value of the name team!

 ‘sum’
# Create a string: team
team = "teen titans"
 ‘range’
# Define change_team()
def change_team():  ‘array’
"""Change the value of the global variable team."""
 ‘tuple’
# Use team in global scope Nested Functions I
global team
 Complete the function header of the nested function with the function
name inner() and a single parameter word.
# Change the value of team in global: team  Complete the return value: each element of the tuple should be a call
team = "justice league" to inner(), passing in the parameters from three_shouts() as arguments
to each call.
# Print team
print(team) # Define three_shouts
# Call change_team() def three_shouts(word1, word2, word3):
change_team() """Returns a tuple of strings
# Print team concatenated with '!!!'."""
print(team)
# Define inner
Python’s built-in scope
def inner(word):
Here you’re going to check out Python’s built-in scope, which is really just a
built-in module called builtins. However, to query builtins, you’ll need """Returns a string concatenated with '!!!'."""
to import builtins ‘because the name builtins is not itself built in…No, I’m return word + '!!!'
serious!’ (Learning Python, 5th edition, Mark Lutz). After executing import
# Call echo: twice
# Return a tuple of strings twice = echo(2)
return (inner(word1), inner(word2), inner(word3)) # Call echo: thrice
# Call three_shouts() and print thrice = echo(3)
print(three_shouts('a', 'b', 'c')) # Call twice() and thrice() then print
print(twice('hello'), thrice('hello'))
Nested Functions II
The keyword nonlocal and nested functions
 Complete the function header of the inner function with the function
name inner_echo() and a single parameter word1.  Assign to echo_word the string word, concatenated with itself.
 Complete the function echo() so that it returns inner_echo.  Use the keyword nonlocal to alter the value of echo_word in the
 We have called echo(), passing 2 as an argument, and assigned the enclosing scope.
resulting function to twice. Your job is to call echo(), passing 3 as an  Alter echo_word to echo_word concatenated with ‘!!!’.
argument. Assign the resulting function to thrice.  Call the function echo_shout(), passing it a single argument ‘hello’.
 Hit Submit to call twice() and thrice() and print the results.

# Define echo_shout()def echo_shout(word):


# Define echo
"""Change the value of a nonlocal variable"""
def echo(n):
"""Return the inner_echo function."""
# Concatenate word with itself: echo_word
echo_word = word*2
# Define inner_echo
def inner_echo(word1):
# Print echo_word
"""Concatenate n copies of word1."""
print(echo_word)
echo_word = word1 * n
return echo_word
# Define inner function shout()
def shout():
# Return inner_echo
"""Alter a variable in the enclosing scope"""
return inner_echo
# Use echo_word in nonlocal scope
nonlocal echo_word # Concatenate echo copies of word1 using *: echo_word
echo_word = word1 * echo
# Change echo_word to echo_word concatenated with '!!!'
echo_word = echo_word + '!!!' # Concatenate '!!!' to echo_word: shout_word
shout_word = echo_word + '!!!'
# Call function shout()
shout() # Return shout_word
return shout_word
# Print echo_word # Call shout_echo() with "Hey": no_echo
print(echo_word) no_echo = shout_echo("Hey")
# Call function echo_shout() with argument 'hello' # Call shout_echo() with "Hey" and echo=5: with_echo
echo_shout('hello') with_echo = shout_echo("Hey", echo=5)
# Print no_echo and with_echo
Functions with one default argument
print(no_echo)
 Complete the function header with the function name shout_echo. It print(with_echo)
accepts an argument word1 and a default argument echo with default
value 1, in that order. Functions with multiple default arguments
 Use the * operator to concatenate echo copies of word1. Assign the
result to echo_word.  Complete the function header with the function name shout_echo. It
 Call shout_echo() with just the string, "Hey". Assign the result accepts an argument word1, a default argument echo with default
to no_echo. value 1 and a default argument intense with default value False, in
 Call shout_echo() with the string "Hey" and the value 5 for the default that order.
argument, echo. Assign the result to with_echo.  In the body of the if statement, make the string
object echo_word upper case by applying the method .upper() on it.
# Define shout_echodef shout_echo(word1, echo=1):  Call shout_echo() with the string, "Hey", the value 5 for echo and the
value True for intense. Assign the result to with_big_echo.
"""Concatenate echo copies of word1 and three
 Call shout_echo() with the string "Hey" and the
exclamation marks at the end of the string.""" value True for intense. Assign the result to big_no_echo.
# Define shout_echo Functions with variable-length arguments (*args)

def shout_echo(word1, echo=1, intense=False):  Complete the function header with the function name gibberish. It
"""Concatenate echo copies of word1 and three accepts a single flexible argument *args.
 Initialize a variable hodgepodge to an empty string.
exclamation marks at the end of the string."""  Return the variable hodgepodge at the end of the function body.
 Call gibberish() with the single string, "luke". Assign the result
to one_word.
# Concatenate echo copies of word1 using *: echo_word
 Hit the Submit button to call gibberish() with multiple arguments and
echo_word = word1 * echo to print the value to the Shell.

# Define gibberishdef gibberish(*args):


# Make echo_word uppercase if intense is True
"""Concatenate strings in *args together."""
if intense is True:
# Make uppercase and concatenate '!!!': echo_word_new
# Initialize an empty string: hodgepodge
echo_word_new = echo_word.upper() + '!!!'
hodgepodge = ''
else:
# Concatenate '!!!' to echo_word: echo_word_new
# Concatenate the strings in args
echo_word_new = echo_word + '!!!'
for word in args:
hodgepodge += word
# Return echo_word_new
return echo_word_new
# Return hodgepodge
# Call shout_echo() with "Hey", echo=5 and intense=True: with_big_echo
return hodgepodge
with_big_echo = shout_echo("Hey", echo=5, intense=True)
# Call gibberish() with one string: one_word
# Call shout_echo() with "Hey" and intense=True: big_no_echo
one_word = gibberish("luke")
big_no_echo = shout_echo("Hey", intense=True)
# Call gibberish() with five strings: many_words
# Print with_big_echo and big_no_echo
many_words = gibberish("luke", "leia", "han", "obi", "darth")
print(with_big_echo)
# Print one_word and many_words
print(big_no_echo)
print(one_word) report_status(name="anakin", affiliation="sith lord", status="deceased")
print(many_words)
Bringing it all together (1)
Functions with variable-length keyword arguments (**kwargs)
 Complete the function header by supplying the parameter for a
DataFrame df and the parameter col_name with a default value
 Complete the function header with the function name report_status. It
of 'lang' for the DataFrame column name.
accepts a single flexible argument **kwargs.
 Call count_entries() by passing the tweets_df DataFrame and the
 Iterate over the key-value pairs of kwargs to print out the keys and
column name 'lang'. Assign the result to result1. Note that
values, separated by a colon ‘:’.
since 'lang' is the default value of the col_name parameter, you don’t
 In the first call to report_status(), pass the following keyword-value
have to specify it here.
pairs: name="luke", affiliation="jedi" and status="missing".
 Call count_entries() by passing the tweets_df DataFrame and the
 In the second call to report_status(), pass the following keyword-
column name 'source'. Assign the result to result2.
value pairs: name="anakin", affiliation="sith
lord" and status="deceased".
# Define count_entries()def count_entries(df, col_name='lang'):
# Define report_statusdef report_status(**kwargs): """Return a dictionary with counts of
"""Print out the status of a movie character.""" occurrences as value for each key."""

print("\nBEGIN: REPORT\n") # Initialize an empty dictionary: cols_count


cols_count = {}
# Iterate over the key-value pairs of kwargs
for key, value in kwargs.items(): # Extract column from DataFrame: col
# Print out the keys and values, separated by a colon ':' col = df[col_name]
print(key + ": " + value)
# Iterate over the column in DataFrame
print("\nEND REPORT") for entry in col:
# First call to report_status()
report_status(name="luke", affiliation="jedi", status="missing") # If entry is in cols_count, add 1
# Second call to report_status() if entry in cols_count.keys():
cols_count[entry] += 1 occurrences as value for each key."""

# Else add the entry to cols_count, set the value to 1 #Initialize an empty dictionary: cols_count
else: cols_count = {}
cols_count[entry] = 1
# Iterate over column names in args
# Return the cols_count dictionary for col_name in args:
return cols_count
# Call count_entries(): result1 # Extract column from DataFrame: col
result1 = count_entries(tweets_df, col_name='lang') col = df[col_name]
# Call count_entries(): result2
result2 = count_entries(tweets_df, col_name='source') # Iterate over the column in DataFrame
# Print result1 and result2 for entry in col:
print(result1)
print(result2) # If entry is in cols_count, add 1
if entry in cols_count.keys():
Bringing it all together (2)
cols_count[entry] += 1
 Complete the function header by supplying the parameter for the
DataFrame df and the flexible argument *args. # Else add the entry to cols_count, set the value to 1
 Complete the for loop within the function definition so that the loop
occurs over the tuple args. else:
 Call count_entries() by passing the tweets_df DataFrame and the cols_count[entry] = 1
column name 'lang'. Assign the result to result1.
 Call count_entries() by passing the tweets_df DataFrame and the
column names 'lang' and 'source'. Assign the result to result2. # Return the cols_count dictionary
return cols_count
# Define count_entries()def count_entries(df, *args):
# Call count_entries(): result1
"""Return a dictionary with counts of
result1 = count_entries(tweets_df, 'lang') return words
# Call count_entries(): result2
 Define the lambda function echo_word using the
result2 = count_entries(tweets_df, 'lang', 'source')
variables word1 and echo. Replicate what the original function
# Print result1 and result2 definition for echo_word() does above.
print(result1)  Call echo_word() with the string argument 'hey' and the value 5, in
that order. Assign the call to result.
print(result2)
# Define echo_word as a lambda function: echo_word
Pop quiz on lambda functions
echo_word = (lambda word1, echo: word1 * echo)
 How would you write a lambda function add_bangs that adds three # Call echo_word: result
exclamation points '!!!' to the end of a string a?
 How would you call add_bangs with the argument 'hello'? result = echo_word('hey', 5)
# Print result
print(result)
The lambda function definition is: add_bangs = (a + '!!!'), and the
function call is: add_bangs('hello'). Map() and lambda functions
For example:
The lambda function definition is: add_bangs = (lambda a: a + '!!!'), and nums = [2, 4, 6, 8, 10]
the function call is: add_bangs('hello').

result = map(lambda a: a ** 2, nums)


The lambda function definition is: (lambda a: a + '!!!') = add_bangs, and
the function call is: add_bangs('hello').  In the map() call, pass a lambda function that concatenates the
string '!!!' to a string item; also pass the list of strings, spells. Assign
Writing a lambda function you already know the resulting map object to shout_spells.
 Convert shout_spells to a list and print out the list.
Take a look at this function definition:

def echo_word(word1, echo): # Create a list of strings: spells


"""Concatenate echo copies of word1.""" spells = ['protego', 'accio', 'expecto patronum', 'legilimens']
words = word1 * echo # Use map() to apply a lambda function over spells: shout_spells
shout_spells = map(lambda item: item + '!!!', spells) """Concatenate strings in *args together."""
# Convert shout_spells to a list: shout_spells_list hodgepodge = ''
shout_spells_list = list(shout_spells) for word in args:
# Print the result hodgepodge += word
print(shout_spells_list) return hodgepodge

Filter() and lambda functions  Import the reduce function from the functools module.
 In the reduce() call, pass a lambda function that takes two string
 In the filter() call, pass a lambda function and the list of arguments item1 and item2 and concatenates them; also pass the list
strings, fellowship. The lambda function should check if the number of strings, stark. Assign the result to result. The first argument
of characters in a string member is greater than 6; use to reduce() should be the lambda function and the second argument is
the len() function to do this. Assign the resulting filter object to result. the list stark.
 Convert result to a list and print out the list.
# Import reduce from functoolsfrom functools import reduce
# Create a list of strings: fellowship
# Create a list of strings: stark
fellowship = ['frodo', 'samwise', 'merry', 'pippin', 'aragorn', 'boromir', 'legolas',
stark = ['robb', 'sansa', 'arya', 'brandon', 'rickon']
'gimli', 'gandalf']
# Use reduce() to apply a lambda function over stark: result
# Use filter() to apply a lambda function over fellowship: result
result = reduce(lambda item1, item2: item1 + item2, stark)
result = filter(lambda member: len(member) > 6, fellowship)
# Print the result
# Convert result to a list: result_list
print(result)
result_list = list(result)
# Print result_list Pop quiz about errors
print(result_list) Take a look at the following function calls to len():

Reduce() and lambda functions len('There is a beast in every man and it stirs when you put a sword in his
hand.')
Remember gibberish() from a few exercises back?

# Define gibberish
len(['robb', 'sansa', 'arya', 'eddard', 'jon'])
def gibberish(*args):
len(525600) exclamation marks at the end of the string."""

len(('jaime', 'cersei', 'tywin', 'tyrion', 'joffrey')) # Initialize empty strings: echo_word, shout_words

Which of the function calls raises an error and what type of error is raised? echo_word = ''
shout_words = ''

The call len('There is a beast in every man and it stirs when you put a
sword in his hand.') raises a TypeError. # Add exception handling with try-except
try:
# Concatenate echo copies of word1 using *: echo_word
The call len(['robb', 'sansa', 'arya', 'eddard', 'jon']) raises an IndexError.
echo_word = word1 * echo

The call len(525600) raises a TypeError. # Concatenate '!!!' to echo_word: shout_words


shout_words = echo_word + '!!!'

The call len(('jaime', 'cersei', 'tywin', 'tyrion', 'joffrey')) raises except:


a NameError. # Print error message
print("word1 must be a string and echo must be an integer.")
Error handling with try-except

 Initialize the variables echo_word and shout_words to empty strings. # Return shout_words
 Add the keywords try and except in the appropriate locations for the
exception handling block. return shout_words
 Use the * operator to concatenate echo copies of word1. Assign the # Call shout_echo
result to echo_word.
 Concatenate the string '!!!' to echo_word. Assign the result shout_echo("particle", echo="accelerator")
to shout_words.
Error handling by raising an error
# Define shout_echodef shout_echo(word1, echo=1):
 Complete the if statement by checking if the value of echo is less
"""Concatenate echo copies of word1 and three than 0.
 In the body of the if statement, add a raise statement that raises first 2 characters in a tweet x are ‘RT’. Assign the resulting filter
a ValueError with message 'echo must be greater than or equal to object to result. To get the first 2 characters in a tweet x, use x[0:2].
0' when the value supplied by the user to echo is less than 0. To check equality, use a Boolean filter with ==.
 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)
# Raise an error with raise
# Print all retweets in res_listfor tweet in res_list:
if echo < 0:
print(tweet)
raise ValueError('echo must be greater than or equal to 0')

Bringing it all together (2)


# Concatenate echo copies of word1 using *: echo_word
 Add a try block so that when the function is called with the correct
echo_word = word1 * echo
arguments, it processes the DataFrame and returns a dictionary of
results.
# Concatenate '!!!' to echo_word: shout_word  Add an except block so that when the function is called incorrectly, it
displays the following error message: 'The DataFrame does not have a
shout_word = echo_word + '!!!' ' + col_name + ' column.'.

# Return shout_word # Define count_entries()def count_entries(df, col_name='lang'):


return shout_word """Return a dictionary with counts of
# Call shout_echo occurrences as value for each key."""
shout_echo("particle", echo=5)
# Initialize an empty dictionary: cols_count
Bringing it all together cols_count = {}
Bringing it all together (1)
# Add try block
 In the filter() call, pass a lambda function and the sequence of tweets
as strings, tweets_df['text']. The lambda function should check if the try:
# Extract column from DataFrame: col  If col_name is not a column in the DataFrame df, raise a ValueError
'The DataFrame does not have a ' + col_name + ' column.'.
col = df[col_name]  Call your new function count_entries() to analyze the 'lang' column
of tweets_df. Store the result in result1.
 Print result1. This has been done for you, so hit ‘Submit Answer’ to
# Iterate over the column in DataFrame check out the result. In the next exercise, you’ll see that it raises the
for entry in col: necessary ValueErrors.

# Define count_entries()def count_entries(df, col_name='lang'):


# If entry is in cols_count, add 1
"""Return a dictionary with counts of
if entry in cols_count.keys():
occurrences as value for each key."""
cols_count[entry] += 1
# Else add the entry to cols_count, set the value to 1
# Raise a ValueError if col_name is NOT in DataFrame
else:
if col_name not in df.columns:
cols_count[entry] = 1
raise ValueError('The DataFrame does not have a ' + col_name + '
column.')
# Return the cols_count dictionary
return cols_count # Initialize an empty dictionary: cols_count
cols_count = {}
# Add except block
except: # Extract column from DataFrame: col
print('The DataFrame does not have a ' + col_name + ' column.') col = df[col_name]
# Call count_entries(): result1
result1 = count_entries(tweets_df, 'lang') # Iterate over the column in DataFrame
# Print result1 for entry in col:
print(result1)

Bringing it all together (3) # 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_entries(tweets_df, 'lang')
# Print result1
print(result1)

Bringing it all together: testing your error handling skills


You have just written error handling into your count_entries() function so
that, when the user passes the function a column (as 2nd argument) NOT
contained in the DataFrame (1st argument), a ValueError is thrown. You’re
now going to play with this function: it is loaded into pre-exercise code, as is
the DataFrame tweets_df. Try calling count_entries(tweets_df, 'lang') to
confirm that the function behaves as it should. Then
call count_entries(tweets_df, 'lang1'): what is the last line of the output?

‘ValueError: The DataFrame does not have the requested column.’

‘ValueError: The DataFrame does not have a lang1 column.’

‘TypeError: The DataFrame does not have the requested column.’

You might also like