Python の URL: http://www.python.org/
インストール手順の詳細は: 別ページで説明している.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
python -m pip install -U pip setuptools python -m pip install -U jupyterlab jupyter jupyter-console jupytext spyder
システム Python を使用(インストール操作は不要)
次のコマンドを実行.
sudo apt -yV install python3-dev python3-pip python3-setuptools sudo apt -yV install jupyter-qtconsole jupyter-notebook python3-jupyter-client python3-jupyter-console python3-spyder spyder3
Windows では「python」を使う,Ubuntu では「python3」を使う.
※ 「pip install ...」は,Python パッケージをインストールするための操作.
python -m pip install -U python -m pip install -U numpy pandas seaborn matplotlib
sudo apt update sudo apt -yV install python3-numpy python3-pandas python3-seaborn python3-matplotlib
今回使用する架空のアンケートデータ
次のようなアンケートデータがあるとする.ファイル名は enquete.csv
このアンケートデータのダウンロード手順
https://www.kkaneko.jp/data/od/enquete.csv
※ このWebページの実習が終わったら、このファイルは消す
※ Python でグラフや図を表示したい. Windows では,スタートメニューの「IDLE (Python ...)」,spyder3コマンド,PyCharmが便利である.
import pandas as pd import seaborn as sns x = pd.read_csv('C:/enquete.csv', encoding='SHIFT-JIS')
先頭行にデータ本体がある(先頭行が属性名でない)ときは、「pd.read_csv('hoge.csv', )」のようにする 読み込んだデータの表示 print(x) 読み込んだデータのうち、1列目と 2列目の表示 ※ オブジェクト x には 0列目と 1列目と 2列目がある. print(x.iloc[:,1]) print(x.iloc[:,2]) 読み込んだデータについて、1列目と 2列目の散布図 「plt.style.use('ggplot')」はグラフの書式の設定.「ro」は「赤い丸」という意味. import matplotlib.pyplot as plt plt.style.use('ggplot') plt.plot(x.iloc[:,1], x.iloc[:,2], 'ro') plt.show() 各列について、基本的な情報の表示 head: 先頭部分の表示 shape: サイズ ndim: 次元数 columns: 属性名 info(): 各属性のデータ型 print(x.head()) print(x.info()) print(x.shape) print(x.ndim) print(x.columns) CSV ファイルに書き出し x.to_csv('hoge.csv', header=True, index=False, encoding='SHIFT-JIS')
※ オブジェクト x には 0列目と 1列目と 2列目がある.
print(x.iloc[:,1]) print(x.iloc[:,2])
「plt.style.use('ggplot')」はグラフの書式の設定.「ro」は「赤い丸」という意味.
import matplotlib.pyplot as plt plt.style.use('ggplot') plt.plot(x.iloc[:,1], x.iloc[:,2], 'ro') plt.show()
print(x.head()) print(x.info()) print(x.shape) print(x.ndim) print(x.columns)
x.to_csv('hoge.csv', header=True, index=False, encoding='SHIFT-JIS')
print(x.describe())
import numpy as np pd.to_json('hoge.json') a = pd.read_json('hoge.json')
pd.DataFrame([1, 2, 3])
import numpy as np import pandas as pd pd.DataFrame( np.array([1,2,3]) )
import numpy as np import pandas as pd pd.DataFrame( np.array([[1,2,3], [10,20,30], [100,200,300]]) )
import pandas as pd pd.DataFrame( {'x' : [1, 2, 3], 'y' : [4, 5, 6]} )
plt.hist(x.iloc[:,1]) plt.show() plt.hist(x.iloc[:,2]) plt.show()
2次元ヒストグラム
plt.hist2d(x.iloc[:,1], x.iloc[:,2]) plt.show()
本サイトは金子邦彦研究室のWebページです.サイトマップは,サイトマップのページをご覧下さい. 本サイト内の検索は,サイト内検索のページをご利用下さい.
問い合わせ先: 金子邦彦(かねこ くにひこ)