Skip to content

Instantly share code, notes, and snippets.

@akrisanov
Created July 25, 2018 23:20
Show Gist options
  • Save akrisanov/199e16900c876b65582d42e05e4ca9c8 to your computer and use it in GitHub Desktop.
Save akrisanov/199e16900c876b65582d42e05e4ca9c8 to your computer and use it in GitHub Desktop.
DataCamp: Importing Data in Python (Part 2) https://www.datacamp.com/courses/importing-data-in-python-part-2
import re
import matplotlib.pyplot as plt
import seaborn as sns
def word_in_text(word, text):
word = word.lower()
text = tweet.lower()
match = re.search(word, text)
if match:
return True
return False
# Initialize list to store tweet counts
[clinton, trump, sanders, cruz] = [0, 0, 0, 0]
# Iterate through df, counting the number of tweets in which
# each candidate is mentioned
for index, row in df.iterrows():
clinton += word_in_text('clinton', row['text'])
trump += word_in_text('trump', row['text'])
sanders += word_in_text('sanders', row['text'])
cruz += word_in_text('cruz', row['text'])
# Set seaborn style
sns.set(color_codes=True)
# Create a list of labels:cd
cd = ['clinton', 'trump', 'sanders', 'cruz']
# Plot histogram
ax = sns.barplot(cd, [clinton, trump, sanders, cruz])
ax.set(ylabel="count")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment