#やっていること
e-statより平成30年の各都道府県の人口(千人)をcsvファイルとしてダウンロードし、これをmatplotlibで描画しようとしています。Pythonを使って、まずdata frameとしてcsvファイルを読み込み、encoding='shift_jis'をここで指定しています。そして人口の数から,を抜き去り、いざ棒グラフにしようとすると、日本語部分が全て□□□と言った様に文字化けしてしまいます。
#該当コード・出力データ
CSV
1"表章項目","男女別","人口","全国・都道府県","/時間軸(年)","平成22年","平成27年","平成28年","平成29年","平成30年" 2"人口【千人】","男女計","総人口","全国","","128,057","127,095","126,933","126,706","126,443" 3"人口【千人】","男女計","総人口","北海道","","5,506","5,382","5,352","5,320","5,286" 4"人口【千人】","男女計","総人口","青森県","","1,373","1,308","1,293","1,278","1,263" 5"人口【千人】","男女計","総人口","岩手県","","1,330","1,280","1,268","1,255","1,241" 6"人口【千人】","男女計","総人口","宮城県","","2,348","2,334","2,330","2,323","2,316" 7"人口【千人】","男女計","総人口","秋田県","","1,086","1,023","1,010","996","981" 8"人口【千人】","男女計","総人口","山形県","","1,169","1,124","1,113","1,102","1,090" 9"人口【千人】","男女計","総人口","福島県","","2,029","1,914","1,901","1,882","1,864" 10"人口【千人】","男女計","総人口","茨城県","","2,970","2,917","2,905","2,892","2,877" 11"人口【千人】","男女計","総人口","栃木県","","2,008","1,974","1,966","1,957","1,946" 12"人口【千人】","男女計","総人口","群馬県","","2,008","1,973","1,967","1,960","1,952" 13"人口【千人】","男女計","総人口","埼玉県","","7,195","7,267","7,289","7,310","7,330" 14"人口【千人】","男女計","総人口","千葉県","","6,216","6,223","6,236","6,246","6,255" 15"人口【千人】","男女計","総人口","東京都","","13,159","13,515","13,624","13,724","13,822" 16"人口【千人】","男女計","総人口","神奈川県","","9,048","9,126","9,145","9,159","9,177" 17"人口【千人】","男女計","総人口","新潟県","","2,374","2,304","2,286","2,267","2,246" 18"人口【千人】","男女計","総人口","富山県","","1,093","1,066","1,061","1,056","1,050" 19"人口【千人】","男女計","総人口","石川県","","1,170","1,154","1,151","1,147","1,143" 20"人口【千人】","男女計","総人口","福井県","","806","787","782","779","774" 21"人口【千人】","男女計","総人口","山梨県","","863","835","830","823","817" 22"人口【千人】","男女計","総人口","長野県","","2,152","2,099","2,088","2,076","2,063" 23"人口【千人】","男女計","総人口","岐阜県","","2,081","2,032","2,022","2,008","1,997" 24"人口【千人】","男女計","総人口","静岡県","","3,765","3,700","3,688","3,675","3,659" 25"人口【千人】","男女計","総人口","愛知県","","7,411","7,483","7,507","7,525","7,537" 26"人口【千人】","男女計","総人口","三重県","","1,855","1,816","1,808","1,800","1,791" 27"人口【千人】","男女計","総人口","滋賀県","","1,411","1,413","1,413","1,413","1,412" 28"人口【千人】","男女計","総人口","京都府","","2,636","2,610","2,605","2,599","2,591" 29"人口【千人】","男女計","総人口","大阪府","","8,865","8,839","8,833","8,823","8,813" 30"人口【千人】","男女計","総人口","兵庫県","","5,588","5,535","5,520","5,503","5,484" 31"人口【千人】","男女計","総人口","奈良県","","1,401","1,364","1,356","1,348","1,339" 32"人口【千人】","男女計","総人口","和歌山県","","1,002","964","954","945","935" 33"人口【千人】","男女計","総人口","鳥取県","","589","573","570","565","560" 34"人口【千人】","男女計","総人口","島根県","","717","694","690","685","680" 35"人口【千人】","男女計","総人口","岡山県","","1,945","1,922","1,915","1,907","1,898" 36"人口【千人】","男女計","総人口","広島県","","2,861","2,844","2,837","2,829","2,817" 37"人口【千人】","男女計","総人口","山口県","","1,451","1,405","1,394","1,383","1,370" 38"人口【千人】","男女計","総人口","徳島県","","785","756","750","743","736" 39"人口【千人】","男女計","総人口","香川県","","996","976","972","967","962" 40"人口【千人】","男女計","総人口","愛媛県","","1,431","1,385","1,375","1,364","1,352" 41"人口【千人】","男女計","総人口","高知県","","764","728","721","714","706" 42"人口【千人】","男女計","総人口","福岡県","","5,072","5,102","5,104","5,107","5,107" 43"人口【千人】","男女計","総人口","佐賀県","","850","833","828","824","819" 44"人口【千人】","男女計","総人口","長崎県","","1,427","1,377","1,367","1,354","1,341" 45"人口【千人】","男女計","総人口","熊本県","","1,817","1,786","1,774","1,765","1,757" 46"人口【千人】","男女計","総人口","大分県","","1,197","1,166","1,160","1,152","1,144" 47"人口【千人】","男女計","総人口","宮崎県","","1,135","1,104","1,096","1,089","1,081" 48"人口【千人】","男女計","総人口","鹿児島県","","1,706","1,648","1,637","1,626","1,614" 49"人口【千人】","男女計","総人口","沖縄県","","1,393","1,434","1,439","1,443","1,448"
Python
1import pandas as pd 2import matplotlib.pyplot as plt 3 4df = pd.read_csv('./population.csv', encoding = 'shift_jis', index_col = '全国・都道府県') 5 6df['平成30年'] = pd.to_numeric(df['平成30年'].str.replace(',', '')) 7 8df['平成30年'].plot.bar() 9plt.show()
#問題解決にあたり
Pythonファイルの別の箇所でencodingの引数をとる関数やモジュールがあるのかと自分なりに調べてみましたが、芳しい情報は得られませんでした。もしくは、それ以前の何か違う問題があるのでしょうか。素人質問で申し訳ございませんが、お力添え頂ける箇所がございましたら、ご教授のほどよろしくお願い申し上げます。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/05 12:05