python DataCamp:使用Python导入数据(第2部分)https://www.datacamp.com/courses/importing-data-in-python-part-2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python DataCamp:使用Python导入数据(第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()

以上是关于python DataCamp:使用Python导入数据(第2部分)https://www.datacamp.com/courses/importing-data-in-python-part-2的主要内容,如果未能解决你的问题,请参考以下文章

Python基础总结

python DataCamp:用于数据科学的中级Python https://www.datacamp.com/courses/intermediate-python-for-data-scienc

python DataCamp:Python数据科学工具箱(第2部分)https://www.datacamp.com/courses/python-data-science-toolbox-part

python DataCamp:用Python导入数据(第1部分)

学习笔记之Intermediate Python for Data Science | DataCamp

Python .loc 混淆