Pythonを使用したデータ分析では必須のライブラリ「Pandas」の環境構築を解説します。

Pythonの環境構築にはpipと呼ばれるパッケージ管理システムがよく利用されます。この記事では、pipを使った方法と使わない方法の両方を解説しています。

Pandasとは

Pandasは、Pythonを使ったデータ解析を支援するために開発されたデータ操作のための高速で、簡単に利用できるようにしたライブラリです。

DataFrameSeriesといったオブジェクトを操作することでPythonから直感的にデータ操作ができるようになっています。また、データ分析をするために便利な関数が揃っているので、ファイルの入出力や時系列データの扱いなどがしやすくなっていることが特徴です。

DataFrameSeriesについては以下の記事で詳細を解説しています。

Pandasのデータを格納するオブジェクトDataFrameを理解する /features/pandas-dataframe.html

Pandasの基本的なデータ構造Seriesの基礎と活用方法 /features/pandas-series.html

このライブラリを使用することで、データ解析は素のPythonを利用することに比べて飛躍的に楽になることでしょう。

Pandasのインストール

pipを使ったインストール方法

Pandasのインストールの方法は単純です。以下のコマンドを打ち込むだけで終了です。

$ pip install pandas

あとはpipがお使いの環境に合わせて自動的にインストールしてくれます。

pipが使えない時

pipコマンドは

  • Python2.7.9以降
  • Python3.4以降

のバージョンにはデフォルトで付属しているパッケージ管理システムですが、もし使えない場合はpipをインストールするとPandasのインストールだけでなく、他のパッケージも気軽にインストールすることができます。

$ sudo easy_install pip
Password:
Searching for pip
Best match: pip 9.0.1
Adding pip 9.0.1 to easy-install.pth file
Installing pip3.5 script to /Users/(ユーザー名)/.pyenv/versions/3.5.3/bin
Installing pip3 script to /Users/(ユーザー名)/.pyenv/versions/3.5.3/bin
Installing pip script to /Users/(ユーザー名)/.pyenv/versions/3.5.3/bin

Using /Users/(ユーザー名)/.pyenv/versions/3.5.3/lib/python3.5/site-packages
Processing dependencies for pip
Finished processing dependencies for pip

pipでインストールできているか確かめます。

$ pip

Usage:   
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring
                              environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be
                              used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be
                              used up to 3 times (corresponding to WARNING,
                              ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form
                              [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should
                              attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists:
                              (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
  --trusted-host <hostname>   Mark this host as trusted, even though it does
                              not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file
                              containing the private key and the certificate
                              in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine
                              whether a new version of pip is available for
                              download. Implied with --no-index.

このような表示が出てきたら、正しくインストールできていることになります。

この他にpyenvを利用することで、Pythonのバージョンやプロジェクト毎パッケージを管理することができるので、そちらも興味があれば調べてみてください。

pipを使わない方法

Ubuntu や Debianの場合、以下のコードでインストールできます。

sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose

また、直接easy_installを使用してインストールすることもできます。

$ easy_install pandas

その他の方法も公式サイトで扱われています。