「好きなアイスクリームアンケート」を例に説明します。
# 「#」(シャープ)以降の文字はプログラムに影響しません。
# 図やグラフを図示するためのライブラリをインポートする。
import matplotlib.pyplot as plt
%matplotlib inline
# 数値計算やデータフレーム操作に関するライブラリをインポートする
import numpy as np
import pandas as pd
from pandas.tools import plotting # 高度なプロットを行うツールのインポート
# URL によるリソースへのアクセスを提供するライブラリをインポートする。
# import urllib # Python 2 の場合
import urllib.request # Python 3 の場合
# ウェブ上のリソースを指定する
url = 'https://raw.githubusercontent.com/maskot1977/ipython_notebook/master/toydata/icecream_chosa.txt'
# 指定したURLからリソースをダウンロードし、名前をつける。
# urllib.urlretrieve(url, 'icecream_chosa.txt') # Python 2 の場合
urllib.request.urlretrieve(url, 'icecream_chosa.txt') # Python 3 の場合
('icecream_chosa.txt', <http.client.HTTPMessage at 0x10b271780>)
# データの読み込み
df1 = pd.read_csv('icecream_chosa.txt', sep=' ', index_col=0)
df1
gender | age | birth_order | frequency | vanilla | strawberry | milk_tea | macadamia_nuts | cookie | chocolate | ... | caramel | walnut | cassis | chocolate_chips | orange | green_tea | marron | chocolate_mint | adzuki-bean | Unnamed: 26 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ID | |||||||||||||||||||||
1 | 2 | 20 | 2 | 5 | 7 | 7 | 8 | 3 | 9 | 9 | ... | 9 | 3 | 3 | 9 | 7 | 8 | 8 | 1 | 7 | NaN |
2 | 2 | 21 | 2 | 1 | 7 | 8 | 9 | 9 | 9 | 9 | ... | 7 | 8 | 9 | 9 | 4 | 7 | 7 | 8 | 9 | NaN |
3 | 2 | 21 | 3 | 2 | 7 | 4 | 3 | 3 | 6 | 4 | ... | 6 | 3 | 3 | 6 | 7 | 3 | 7 | 3 | 3 | NaN |
4 | 2 | 21 | 4 | 3 | 9 | 6 | 6 | 5 | 8 | 6 | ... | 9 | 5 | 4 | 8 | 9 | 1 | 2 | 1 | 3 | NaN |
5 | 2 | 21 | 4 | 2 | 9 | 5 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| 8 | 7 | NaN |
29 | 2 | 21 | 3 | 4 | 6 | 7 | 7 | 8 | 8 | 9 | ... | 9 | 8 | 7 | 9 | 7 | 7 | 5 | 5 | 3 | NaN |
30 | 2 | 21 | 4 | 4 | 7 | 7 | 7 | 7 | 9 | 9 | ... | 9 | 5 | 5 | 9 | 6 | 9 | 7 | 9 | 6 | NaN |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
51 | 1 | 19 | 2 | 6 | 7 | 8 | 6 | 3 | 5 | 6 | ... | 5 | 2 | 3 | 8 | 5 | 8 | 3 | 8 | 2 | NaN |
52 | 1 | 21 | 1 | 2 | 9 | 4 | 5 | 3 | 7 | 7 | ... | 6 | 2 | 4 | 9 | 8 | 7 | 5 | 5 | 9 | NaN |
53 | 1 | 20 | 1 | 5 | 9 | 8 | 7 | 6 | 9 | 8 | ... | 9 | 7 | 6 | 9 | 5 | 3 | 9 | 1 | 9 | NaN |
54 | 1 | 21 | 4 | 5 | 8 | 8 | 7 | 5 | 7 | 7 | ... | 4 | 4 | 4 | 7 | 6 | 3 | 4 | 6 | 5 | NaN |
55 | 1 | 19 | 4 | 4 | 7 | 7 | 8 | 7 | 6 | 9 | ... | 6 | 5 | 9 | 9 | 7 | 9 | 7 | 8 | 9 | NaN |
56 | 1 | 20 | 2 | 2 | 5 | 7 | 5 | 5 | 5 | 9 | ... | 6 | 5 | 5 | 9 | 9 | 8 | 5 | 9 | 8 | NaN |
57 | 1 | 22 | 1 | 3 | 9 | 9 | 9 | 9 | 9 | 6 | ... | 8 | 6 | 6 | 8 | 7 | 9 | 7 | 1 | 9 | NaN |
58 | 1 | 19 | 1 | 3 | 9 | 3 | 5 | 7 | 6 | 3 | ... | 1 | 6 | 3 | 8 | 2 | 7 | 1 | 1 | 3 | NaN |
59 | 1 | 20 | 2 | 3 | 7 | 7 | 7 | 4 | 6 | 6 | ... | 8 | 3 | 6 | 7 | 7 | 8 | 6 |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 5 | 9 | 7 | 3 | 7 | 6 | 8 | NaN |
72 | 1 | 24 | 1 | 2 | 6 | 7 | 5 | 6 | 4 | 8 | ... | 2 | 6 | 8 | 4 | 7 | 8 | 3 | 2 | 6 | NaN |
73 | 1 | 20 | 2 | 2 | 6 | 6 | 5 | 8 | 8 | 7 | ... | 7 | 8 | 7 | 8 | 5 | 8 | 8 | 8 | 7 | NaN |
74 | 1 | 20 | 2 | 5 | 8 | 8 | 4 | 8 | 9 | 7 | ... | 4 | 8 | 8 | 9 | 8 | 9 | 6 | 9 | 4 | NaN |
75 | 1 | 21 | 2 | 6 | 4 | 6 | 2 | 3 | 6 | 6 | ... | 2 | 3 | 2 | 6 | 8 | 1 | 1 | 2 | 2 | NaN |
76 | 1 | 20 | 2 | 4 | 5 | 6 | 4 | 9 | 6 | 7 | ... | 5 | 5 | 5 | 7 | 7 | 4 | 4 | 4 | 4 | NaN |
77 | 1 | 20 | 3 | 6 | 7 | 5 | 6 | 6 | 8 | 9 | ... | 9 | 5 | 5 | 9 | 7 | 6 | 5 | 6 | 7 | NaN |
78 | 1 | 23 | 1 | 5 | 4 | 8 | 8 | 9 | 9 | 9 | ... | 8 | 9 | 5 | 9 | 9 | 9 | 4 | 9 | 9 | NaN |
79 | 1 | 23 | 4 | 7 | 9 | 7 | 7 | 5 | 4 | 6 | ... | 7 | 4 | 5 | 8 | 8 | 5 | 5 | 8 | 4 | NaN |
80 | 1 | 22 | 2 | 2 | 8 | 5 | 5 | 2 | 5 | 4 | ... | 5 | 5 | 5 | 5 | 7 | 1 | 1 | 5 | 5 | NaN |
80 rows × 26 columns
# ヒストグラムを作成する
df1.dropna(axis=1).hist(figsize=(20,20))
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x112161320>, <matplotlib.axes._subplots.AxesSubplot object at 0x113147048>, <matplotlib.axes._subplots.AxesSubplot object at 0x113423cc0>, <matplotlib.axes._subplots.AxesSubplot object at 0x113458128>, <matplotlib.axes._subplots.AxesSubplot object at 0x1134969b0>], [<matplotlib.axes._subplots.AxesSubplot object at 0x1134cbdd8>, <matplotlib.axes._subplots.AxesSubplot object at 0x113514f98>, <matplotlib.axes._subplots.AxesSubplot object at 0x113523630>, <matplotlib.axes._subplots.AxesSubplot object at 0x1135a3e48>, <matplotlib.axes._subplots.AxesSubplot object at 0x1135f1048>], [<matplotlib.axes._subplots.AxesSubplot object at 0x113627a58>, <matplotlib.axes._subplots.AxesSubplot object at 0x11366fc18>, <matplotlib.axes._subplots.AxesSubplot object at 0x1136ac940>, <matplotlib.axes._subplots.AxesSubplot object at 0x1138127f0>, <matplotlib.axes._subplots.AxesSubplot object at 0x113821978>], [<matplotlib.axes._subplots.AxesSubplot object at 0x1138a2048>, <matplotlib.axes._subplots.AxesSubplot object at 0x1138eb400>, <matplotlib.axes._subplots.AxesSubplot object at 0x113a266d8>, <matplotlib.axes._subplots.AxesSubplot object at 0x113a6f898>, <matplotlib.axes._subplots.AxesSubplot object at 0x113aaf160>], [<matplotlib.axes._subplots.AxesSubplot object at 0x113af7cc0>, <matplotlib.axes._subplots.AxesSubplot object at 0x113ab9860>, <matplotlib.axes._subplots.AxesSubplot object at 0x113c82198>, <matplotlib.axes._subplots.AxesSubplot object at 0x113cc7f28>, <matplotlib.axes._subplots.AxesSubplot object at 0x113e0b668>]], dtype=object)
# 左側4列を除外してヒストグラムを作成する
df1.dropna(axis=1).iloc[:, 4:-1].hist(figsize=(20,20))
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x116562dd8>, <matplotlib.axes._subplots.AxesSubplot object at 0x116b74710>, <matplotlib.axes._subplots.AxesSubplot object at 0x117586390>, <matplotlib.axes._subplots.AxesSubplot object at 0x1175bbdd8>], [<matplotlib.axes._subplots.AxesSubplot object at 0x1177410f0>, <matplotlib.axes._subplots.AxesSubplot object at 0x117776cc0>, <matplotlib.axes._subplots.AxesSubplot object at 0x1177bec18>, <matplotlib.axes._subplots.AxesSubplot object at 0x1177ce358>], [<matplotlib.axes._subplots.AxesSubplot object at 0x11794d358>, <matplotlib.axes._subplots.AxesSubplot object at 0x117997710>, <matplotlib.axes._subplots.AxesSubplot object at 0x1179d19e8>, <matplotlib.axes._subplots.AxesSubplot object at 0x117a1cba8>], [<matplotlib.axes._subplots.AxesSubplot object at 0x117a568d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x117aa4fd0>, <matplotlib.axes._subplots.AxesSubplot object at 0x117a70860>, <matplotlib.axes._subplots.AxesSubplot object at 0x117b28eb8>], [<matplotlib.axes._subplots.AxesSubplot object at 0x117b71f60>, <matplotlib.axes._subplots.AxesSubplot object at 0x117bb2ef0>, <matplotlib.axes._subplots.AxesSubplot object at 0x117d00898>, <matplotlib.axes._subplots.AxesSubplot object at 0x117d3e080>]], dtype=object)