Get startedGet started for free

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

View Course

Exercise instructions

  • Define the function count_entries(), which has two parameters. The first parameter is df for the DataFrame and the second is col_name for the column name.
  • 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 loop variable entry in your code.
  • Return the langs_count dictionary from inside the count_entries() function.
  • Call the count_entries() function by passing to it tweets_df and the name of the column, 'lang'. Assign the result of the call to the variable result.

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)
Edit and Run Code