Bringing it all together (2)
Wow, you've just generalized your Twitter language analysis that you did in the previous chapter to include a default argument for the column name. You're now going to generalize this function one step further by allowing the user to pass it a flexible argument, that is, in this case, as many column names as the user would like!
Once again, for your convenience, pandas
has been imported as pd
and the 'tweets.csv'
file has been imported into the DataFrame tweets_df
. Parts of the code from your previous work are also provided.
This exercise is part of the course
Introduction to Functions in Python
Exercise instructions
- Complete the function header by supplying the parameter for the DataFrame
df
and the flexible argument*args
. - Complete the
for
loop within the function definition so that the loop occurs over the tupleargs
. - Call
count_entries()
by passing thetweets_df
DataFrame and the column name'lang'
. Assign the result toresult1
. - Call
count_entries()
by passing thetweets_df
DataFrame and the column names'lang'
and'source'
. Assign the result toresult2
.
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: cols_count
cols_count = {}
# Iterate over column names in args
for col_name in ____:
# 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_entries(____, ____)
# Call count_entries(): result2
result2 = count_entries(____, ____, ____)
# Print result1 and result2
print(result1)
print(result2)