Python の URL: http://www.python.org/
インストール手順の詳細は: 別ページで説明している.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
python -m pip install -U pip setuptools python -m pip install -U jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
システム Python を使用(インストール操作は不要)
端末で,次のコマンドを実行.
sudo apt -y install python3-dev python3-pip python3-setuptools sudo pip3 uninstall ptyprocess sniffio terminado tornado jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder sudo apt -y install jupyter jupyter-qtconsole spyder3 sudo apt -y install python3-ptyprocess python3-sniffio python3-terminado python3-tornado sudo pip3 install jupyterlab nteract_on_jupyter
python -m pip install -U numpy pandas pandasql seaborn matplotlib
sudo apt -y install python3-numpy python3-pandas python3-seaborn python3-matplotlib sudo python3 -m pip install -U pandasql
Python プログラムを動かして,結果を見たい.
Jupyter Qt Console, spyder, PyCharm, PyScripter が便利である. Windows では,スタートメニューの「IDLE (Python ...)」も便利である.
import pandas as pd import seaborn as sns iris = sns.load_dataset('iris') titanic = sns.load_dataset('titanic')
print(iris.head()) print(titanic.head())
列を1つ選ぶことで、グループを作り、各グループの要素数を求める
pandas の groupby を使う場合
import pandas as pd import seaborn as sns iris = sns.load_dataset('iris') titanic = sns.load_dataset('titanic') print( iris.groupby('species').size() ) print( titanic.groupby('embark_town').size() )
pandasql と SQL を使う場合
import pandas as pd import seaborn as sns from pandasql import sqldf iris = sns.load_dataset('iris') titanic = sns.load_dataset('titanic') pysqldf = lambda q: sqldf(q, globals()) print( pysqldf("SELECT species, count(*) FROM iris group by species;") ); print( pysqldf("SELECT embark_town, count(*) FROM titanic group by embark_town;") );
列を1つ選ぶことで、グループを作り、各グループの最大、最小、平均、中央値、和を求める
pandas の groupby を使う場合
import pandas as pd import seaborn as sns iris = sns.load_dataset('iris') titanic = sns.load_dataset('titanic') print( iris.groupby('species').max() ) print( titanic.groupby('embark_town').max() ) print( iris.groupby('species').min() ) print( titanic.groupby('embark_town').min() ) print( iris.groupby('species').mean() ) print( titanic.groupby('embark_town').mean() ) print( iris.groupby('species').median() ) print( titanic.groupby('embark_town').median() ) print( iris.groupby('species').sum() ) print( titanic.groupby('embark_town').sum() )
pandasql と SQL を使う場合
iris データセットで,各グループの最大値を求める場合のみを載せる.
import pandas as pd import seaborn as sns from pandasql import sqldf iris = sns.load_dataset('iris') titanic = sns.load_dataset('titanic') pysqldf = lambda q: sqldf(q, globals()) print( pysqldf("SELECT species, max(sepal_length), max(sepal_width), max(petal_length), max(petal_width) FROM iris group by species;") );
本サイトは金子邦彦研究室のWebページです.サイトマップは,サイトマップのページをご覧下さい. 本サイト内の検索は,サイト内検索のページをご利用下さい.
問い合わせ先: 金子邦彦(かねこ くにひこ)