前提・実現したいこと
pythonの学習を始めたばかりです。
サンプルの課題独学中です。
都道府県別の人口データをcsv形式で読み込み、pandasとmatplotを用い、H28とH12差を計算し上位10位を抽出し、棒グラフで表示しようと思っていますが、グラフ表示でKeyErrorが出ます。
恥ずかしながら、教えて下さるとたすかります。
以下のエラーメッセージが発生しました。
KeyError Traceback (most recent call last)
<ipython-input-7-724a8255b16c> in <module>()
14 top10 = df[0:10]
15 # グラフで描画
---> 16 top10.plot.bar(y=["Increase and decrease"], x=["Prefectures"])
17 top10
KeyError: "['Prefectures'] not in index"
該当のソースコード(jupyternotebook と Python3)
%matplotlib inline
import pandas as pd
df = pd.read_csv("population.csv", encoding="SHIFT_JIS")
増減(Increase and decrease)を調べる --- (*1)平成28年と12年の差を計算し演算した結果を「Increase and decrease」という列を作って代入
df['Increase and decrease'] = df["平成28年"] - df["平成12年"]
並び替え --- (*2)
df = df.sort_values(by=["Increase and decrease"], ascending=False)
上位10位を得る 都道府県(Prefectures)--- Pandasで任意の範囲の行を取り出す
top10 = df[0:10]
棒グラフで描画
top10.plot.bar(y=["Increase and decrease"], x=["Prefectures"])
top10
試したこと
top10 = df[0:10]のラインまではエラーが出ないようです。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/13 05:04
2018/12/13 05:34
2018/12/13 07:54