LoginSignup
3
8

More than 5 years have passed since last update.

Jupyter Notebook (IPython Notebook) で遊んでみる

Posted at

ref: http://qiita.com/shizuma/items/027167c6257f1c9d2a6f

上の記事に従って, anaconda (Python & 各種パッケージの詰め合わせ) の最新版を pyenv 経由でインストールする. (pyenv は homebrew などで入れる)

pyenv install anaconda3-2.4.1
pyenv global anaconda3-2.4.1

起動は任意のフォルダに移動して

ipython notebook

すると, localhost:8888 が立ち上がる.

shortcuts

esc 状態で以下のショートカットが使える. vi のコマンドモードライク

ショートカット 機能
h ヘルプ
b 新しい cell 作成
dd 現在の cell の削除
shift+enter 現在の cell の実行
s 保存
y code cell へ
m markdown cell へ

テーマの変更

jupyterthemes を使う.

pip install jupyterthemes
jt -t onedork

mysql に接続しデータを表示する

ref: http://qiita.com/shrkw/items/c38def7d60b0099b0c55

pip install pymysql
import pymysql

conn = pymysql.connect(host='localhost',
                       user='root',
                       password='',
                       db='mydatabase',
                       charset='utf8mb4',
                       cursorclass=pymysql.cursors.DictCursor)

ユーザー情報の取得

pandas の dataframe にそのまま変換したいので, pandas の sql ライブラリを使う
ほか, 必要そうなライブラリも import しておく

import pandas
from pandas.io import sql
import numpy
import pylab
import pymysql
import matplotlib.pyplot as plt
%matplotlib inline

users 情報を取る

users = sql.read_sql('''SELECT users.* FROM users''', conn)

pandas.DataFrame

基本統計量を出す

users.describe()

ヒストグラム一覧を出す

users.hist()

ユーザーの年齢をヒストグラムとして出す(ビンを 30 本とする)

# http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.hist.html
users.age.hist(bins=30)
3
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
8