Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Define count_entries()
- def count_entries(df, *args):
- """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 args:
- # 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(tweets_df, "lang")
- # Call count_entries(): result2
- result2 = count_entries(tweets_df, "lang", "source")
- # Print result1 and result2
- print(result1)
- print(result2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement