Bringing it all together (2)
Great job! You've now defined the functionality for iterating over entries in a column and building a dictionary with keys the names of languages and values the number of tweets in the given language.
In this exercise, you will define a function with the functionality you developed in the previous exercise, return the resulting dictionary from within the function, and call the function with the appropriate arguments.
For your convenience, the pandas package has been imported as pd
and the 'tweets.csv'
file has been imported into the tweets_df
variable.
This exercise is part of the course
Introduction to Functions in Python
Exercise instructions
- Define the function
count_entries()
, which has two parameters. The first parameter isdf
for the DataFrame and the second iscol_name
for the column name. - Complete the bodies of the
if-else
statements in thefor
loop: if the key is in the dictionarylangs_count
, add1
to its current value, else add the key tolangs_count
and set its value to1
. Use the loop variableentry
in your code. - Return the
langs_count
dictionary from inside thecount_entries()
function. - Call the
count_entries()
function by passing to ittweets_df
and the name of the column,'lang'
. Assign the result of the call to the variableresult
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define count_entries()
def ____(____, ____):
"""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():
____
# Else add the language to langs_count, set the value to 1
else:
____
# Return the langs_count dictionary
# Call count_entries(): result
# Print the result
print(result)