Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. # ---
  2. # data camp example
  3. # ---
  4.  
  5. # Import pandas
  6. import pandas as pd
  7.  
  8. # Import Twitter data as DataFrame: df
  9. df = pd.read_csv('tweets.csv')
  10.  
  11. # Initialize an empty dictionary: langs_count
  12. langs_count = {}
  13.  
  14. # Extract column from DataFrame: col
  15. col = df['lang']
  16.  
  17. # Iterate over lang column in DataFrame
  18. for entry in col:
  19.  
  20. # If the language is in langs_count, add 1
  21. if entry in langs_count.keys():
  22. langs_count[entry] += 1
  23. # Else add the language to langs_count, set the value to 1
  24. else:
  25. langs_count[entry] = 1
  26.  
  27. # Print the populated dictionary
  28. print(langs_count)
  29.  
  30. # ----------------------
  31. ## example 2
  32. # ----------------------
  33. # Define count_entries()
  34. def count_entries(df, col_name):
  35. """Return a dictionary with counts of
  36. occurrences as value for each key."""
  37.  
  38. # Initialize an empty dictionary: langs_count
  39. langs_count = {}
  40.  
  41. # Extract column from DataFrame: col
  42. col = df[col_name]
  43.  
  44. # Iterate over lang column in DataFrame
  45. for entry in col:
  46.  
  47. # If the language is in langs_count, add 1
  48. if entry in langs_count.keys():
  49. langs_count[entry] += 1
  50. # Else add the language to langs_count, set the value to 1
  51. else:
  52. langs_count[entry] = 1
  53.  
  54. # Return the langs_count dictionary
  55. return langs_count
  56.  
  57. # Call count_entries(): result
  58. result = count_entries(tweets_df, 'lang')
  59.  
  60. # Print the result
  61. print(result)
  62.  
  63. # ---
  64. # example 3 - generalized function
  65. # ----
  66. # Define count_entries()
  67. def count_entries(df, col_name = 'lang'):
  68. """Return a dictionary with counts of
  69. occurrences as value for each key."""
  70.  
  71. # Initialize an empty dictionary: cols_count
  72. cols_count = {}
  73.  
  74. # Extract column from DataFrame: col
  75. col = df[col_name]
  76.  
  77. # Iterate over the column in DataFrame
  78. for entry in col:
  79.  
  80. # If entry is in cols_count, add 1
  81. if entry in cols_count.keys():
  82. cols_count[entry] += 1
  83.  
  84. # Else add the entry to cols_count, set the value to 1
  85. else:
  86. cols_count[entry] = 1
  87.  
  88. # Return the cols_count dictionary
  89. return cols_count
  90.  
  91. # Call count_entries(): result1
  92. result1 = count_entries(tweets_df)
  93.  
  94. # Call count_entries(): result2
  95. result2 = count_entries(tweets_df, 'source')
  96.  
  97. # Print result1 and result2
  98. print(result1)
  99. print(result2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement