Skip to content

Instantly share code, notes, and snippets.

@hitowaft
Last active April 11, 2017 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitowaft/1ce4daa0e5aaf4ecdbc902656c8436fb to your computer and use it in GitHub Desktop.
Save hitowaft/1ce4daa0e5aaf4ecdbc902656c8436fb to your computer and use it in GitHub Desktop.
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# In[2]:
tweet_df = pd.read_csv("/hoge/tweets.csv", parse_dates=["timestamp"]) #ここにcsvファイルのパスを入れる
tweet_df = tweet_df.set_index(["timestamp"])
tweet_df = tweet_df.reindex(columns=["text"])
# In[3]:
keyword = {"Yohei":"洋平|ようぺ"} #ここにキーワードを入れる
for k, v in keyword.items():
tweet_df["{}".format(k)] = tweet_df["text"].str.contains("{}".format(v))
new_df = tweet_df.drop(["text"], axis=1)
new_df = new_df.resample("M").sum()
# In[4]:
graph = []
for i in range(1,13):
  graph.append(new_df.loc[(new_df.index.month == i)].mean())
graph = pd.DataFrame(graph, index=np.arange(1,13))
graph.plot(kind="bar", legend=True)
plt.show()
#追記:以下は必要に応じて使ってください
"""
for i in keyword.values():
print(tweet_df["text"][tweet_df["text"].str.contains("{}". format(i))])
   
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment