Advertisement
jorgefernandes

Untitled

Feb 19th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. # Define count_entries()
  2. def count_entries(df, *args):
  3. """Return a dictionary with counts of
  4. occurrences as value for each key."""
  5.  
  6. #Initialize an empty dictionary: cols_count
  7. cols_count = {}
  8.  
  9. # Iterate over column names in args
  10. for col_name in args:
  11.  
  12. # Extract column from DataFrame: col
  13. col = df[col_name]
  14.  
  15. # Iterate over the column in DataFrame
  16. for entry in col:
  17.  
  18. # If entry is in cols_count, add 1
  19. if entry in cols_count.keys():
  20. cols_count[entry] += 1
  21.  
  22. # Else add the entry to cols_count, set the value to 1
  23. else:
  24. cols_count[entry] = 1
  25.  
  26. # Return the cols_count dictionary
  27. return cols_count
  28.  
  29. # Call count_entries(): result1
  30. result1 = count_entries(tweets_df, "lang")
  31.  
  32. # Call count_entries(): result2
  33. result2 = count_entries(tweets_df, "lang", "source")
  34.  
  35. # Print result1 and result2
  36. print(result1)
  37. print(result2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement