■使用言語とライブラリ
・Python
・Google Colaboratory
・matplotlib
■本文
Python実践データ分析100本ノックという本の第一章(10ノック目)の中に、matplotlibを使用して、グラフをプロットする作業があるのですが、本内の同じコードを記載しても、KeyErrorがでて、実行されず困っております。
この前の記述はすべて、正常に実行され、カラム名や、変数の指定ミスなどないはずなのですが、
エラーが出ているので、もしかしたら、使っているGoogle Colaboratoryがmatplotlibでうまく実行されないとかあるのではないかと思っているのですが、そのようなことはありますでしょうか?
なにかおわかりになる方がいらっしゃれば、
ご教示いただけますと幸いです。
#matplotlibをインポートする import matplotlib.pyplot as plt #アウトプットでグラフを作るために記述が必要 %matplotlib inline #商品ごとの売上推移をプロットする plt.plot(list(graph_data.index), graph_data["PC-A"], label = "PC-A") plt.plot(list(graph_data.index), graph_data["PC-B"], label = "PC-B") plt.plot(list(graph_data.index), graph_data["PC-C"], label = "PC-C") plt.plot(list(graph_data.index), graph_data["PC-D"], label = "PC-D") plt.plot(list(graph_data.index), graph_data["PC-E"], label = "PC-E") plt.legend()
▼エラー結果
KeyError Traceback (most recent call last)
<ipython-input-56-e470ffb614d4> in <module>()
7
8 #商品ごとの売上推移をプロットする
----> 9 plt.plot(list(graph_data.index), graph_data[["PC-A"]], label = "PC-A")
10 plt.plot(list(graph_data.index), graph_data["PC-B"], label = "PC-B")
11 plt.plot(list(graph_data.index), graph_data["PC-C"], label = "PC-C")
2 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py in getitem(self, key)
2910 if is_iterator(key):
2911 key = list(key)
-> 2912 indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
2913
2914 # take() does not accept boolean indexers
/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
1238 # Have the index compute an indexer or return None
1239 # if it cannot handle:
-> 1240 indexer, keyarr = ax._convert_listlike_indexer(key)
1241 # We only act on all found values:
1242 if indexer is not None and (indexer != -1).all():
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/multi.py in _convert_listlike_indexer(self, keyarr)
2398 mask = check == -1
2399 if mask.any():
-> 2400 raise KeyError(f"{keyarr[mask]} not in index")
2401
2402 return indexer, keyarr
KeyError: "['PC-A'] not in index"
回答1件
あなたの回答
tips
プレビュー