前提
Twitter の Progress Bar 2023 (@ProgressBar202_) のツイートを解析しようとしています。以下の形式でツイートの情報を取得しました。
Python
1tweets = [ 2 "Progress Bar 2023\n@ProgressBar202_\n·\n2023年1月1日\n2023 is 1% complete.\n1,865\n7.7万\n61.3万", 3 "Progress Bar 2023\n@ProgressBar202_\n·\n2023年1月1日\n2022 is 100% complete.\n902\n5.9万\n50.5万", 4 "Progress Bar 2023\n@ProgressBar202_\n·\n2022年12月28日\n2022 is 99% complete.\n1,083\n4.8万\n28.2万", 5 "Progress Bar 2023\n@ProgressBar202_\n·\n2022年12月25日\n2022 is 98% complete.\n577\n2.1万\n19.9万", 6... 7]
↑のデータのフル版はこちらに貼っておきます。
実現したいこと
先ほど取得した情報を regex で整形し、pandas で表にしようと試みました。以下のコードの通りです。
Python
1import re 2import pandas as pd 3 4table = {"year": [], "percent": [], "reply": [], "RT": [], "fab": []} 5def parse(x): 6 if x[-1] == "万": 7 return int(float(x[:-1]) * 10000) 8 elif "," in x: 9 return int(x.replace(",","")) 10 else: 11 return int(x) 12 13for tw in tweets: 14 m = re.search(r"(\d+).* is (\d+)%.*\n(.*)\n(.*)\n(.*)", tw) 15 table["year"].append(int(m[1])) 16 table["percent"].append(int(m[2])) 17 table["reply"].append(parse(m[3])) 18 table["RT"].append(parse(m[4])) 19 table["fab"].append(parse(m[5])) 20 21df = pd.DataFrame(table)
それを以下のコードで可視化したいと思っています。
Python
1plt.figure(figsize=(15, 10)) 2plt.subplot(2,1,1) 3for year in [2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023]: 4 data = df[df.year == year].sort_values("percent") 5 plt.plot(data.percent, data.RT, ".-", label="%d"%year) 6plt.legend() 7plt.xlabel("%") 8plt.xticks(range(0, 101, 10)) 9plt.title("# of RTs") 10plt.subplot(2,1,2) 11for year in [2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023]: 12 data = df[df.year == year].sort_values("percent") 13 plt.plot(data.percent, data.fab, ".-", label="%d"%year) 14plt.legend() 15plt.xlabel("%") 16plt.xticks(range(0, 101, 10)) 17plt.title("# of fabs") 18plt.show()
発生している問題・エラーメッセージ
先ほどのコードを実行すると、
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-840e886fa9d8> in <module> 13 for tw in tweets: 14 m = re.search(r'(\d+).* is (\d+)%.*\n(.*)\n(.*)\n(.*)', tw) ---> 15 table["year"].append(int(m[1])) 16 table["percent"].append(int(m[2])) 17 table["reply"].append(parse(m[3])) TypeError: 'NoneType' object is not subscriptable
とエラーメッセージが出ます。
試したこと
m
の型を確認したところ、NoneType
であることはわかっています。 Stack Overflow 等確認してみましたが解決策等見つけることはできませんでした。皆さんのお力添えをよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー