Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# 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