intoroduction of pandas with python

18

Click here to load reader

Upload: atsushi-hayakawa

Post on 10-May-2015

4.392 views

Category:

Technology


7 download

TRANSCRIPT

Page 1: Intoroduction of Pandas with Python

1

集合知プログラミング勉強会キックオフMtg

Pandas超入門

@gepuro

Page 2: Intoroduction of Pandas with Python

2

自己紹介

● 早川 敦士 (@gepuro)● 電気通信大学システム工学科4年生● 専攻:信頼性工学● 好きな言語:Python,R,AWK● 趣味:花火,テキストマイニング,アニメ● 活動場所:電通大,MMA,iAnalysis,DBCLS

Page 3: Intoroduction of Pandas with Python

3

Pandasとは?

pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

pandasはハイパフォーマンスなライブラリで、Pythonでデータ構造やデータ解析ツールをめっちゃ使いやすい。(超意訳)

http://pandas.pydata.org/ より

Page 4: Intoroduction of Pandas with Python

4

Pandasの役割

● データの加工からモデリングをスムーズに行える。

● R言語のデータフレームに出来るような操作をPythonで行える。

● Pandas自体には多くの統計モデルが実装されていないので、他のライブラリとの連携が必要。

● pandasは、Rへのインターフェースがある。Rpy2との連携

● などなど

Page 5: Intoroduction of Pandas with Python

5

Pandasの情報

● http://pandas.pydata.org/pandas-docs/stable/ にある公式ドキュメントが整っていて見やすい。

● この勉強会では、ここにある情報を主に紹介できたらと思う。

Page 6: Intoroduction of Pandas with Python

6

Pandasのインストール

ソースからgit clone git://github.com/pydata/pandas.gitcd pandaspython setup.py install

ubntuユーザならapt-get install python-pandas

Page 7: Intoroduction of Pandas with Python

7

Pandasの依存ライブラリ

Dependencies

NumPy: 1.6.1 or higher

python-dateutil 1.5

Optional dependencies

SciPy: miscellaneous statistical functions

PyTables: necessary for HDF5-based storage

matplotlib: for plotting

scikits.statsmodels:Needed for parts of pandas.stats

pytz:Needed for time zone support with date_range

Page 8: Intoroduction of Pandas with Python

8

データ構造Dimensions Name Description

1 Series 1D labeled homogeneously-typed array

1 TimeSeries Series with index containing datetimes

2 DataFrame General 2D labeled, size-mutable tabular structure with potentially heterogeneously-typed columns

3 Panel General 3D labeled, also size-mutable array

Page 9: Intoroduction of Pandas with Python

9

Seriesの利用

In [52]: pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"])Out[52]: a -0.904244b 0.870734c -0.217093d 0.123815e 0.356112

In [53]: s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"])

In [54]: s.indexOut[54]: Index([a, b, c, d, e], dtype=object)

Seriesに挿入できるデータ型● a Python dict● an ndarray● a scalar value (like 5)

Page 10: Intoroduction of Pandas with Python

10

Seriesでの計算In [66]: s + sOut[66]: a 0.388344b 3.670871c 1.306869d -0.237199e 3.168135

In [67]: s * 2Out[67]: a 0.388344b 3.670871c 1.306869d -0.237199e 3.168135

In [68]: np.log(s)Out[68]: a -1.639012b 0.607282c -0.425513d NaNe 0.459996

ベクトル計算も楽に出来る!

Rでの操作と同じ感覚!

Page 11: Intoroduction of Pandas with Python

11

DataFrameの利用DataFrameに挿入できるデータ型

● Dict of 1D ndarrays, lists, dicts, or Series● 2-D numpy.ndarray● Structured or record ndarray● A Series● Another DataFrame

In [9]: d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}

In [10]: df = pd.DataFrame(d)

In [11]: dfOut[11]: one twoa 1 1b 2 2c 3 3d NaN 4

Page 12: Intoroduction of Pandas with Python

12

DataFrameでの計算

In [17]: df + dfOut[17]: one twoa 2 2b 4 4c 6 6d NaN 8

In [18]: df + 1Out[18]: one twoa 2 2b 3 3c 4 4d NaN 5

Series同様に、馴染みの操作が出来る。

Page 13: Intoroduction of Pandas with Python

13

DataFrameでの列の追加

In [21]: dfOut[21]: one twoa 1 1b 2 2c 3 3d NaN 4

In [22]: df["three"] = df["one"] * df["two"]

In [23]: dfOut[23]: one two threea 1 1 1b 2 2 4c 3 3 9d NaN 4 NaN

辞書型(連想配列)を扱うように、列を追加出来る。

Page 14: Intoroduction of Pandas with Python

14

DataFrameへのアクセス

In [46]: df["one"]Out[46]: a 1b 2c 3d NaNName: one

In [47]: df.xs("a")Out[47]: one 1two 1three 1Name: a

列でアクセス

行でアクセス

Page 15: Intoroduction of Pandas with Python

15

DataFrameでの便利メソッド

In [91]: df.mean()Out[91]: one 2.000000two 2.500000three 4.666667

In [92]: df.max()Out[92]: one 3two 4three 9

In [93]: df.var()Out[93]: one 1.000000two 1.666667Three 16.333333

In [100]: df.apply(lambda x: x.max() - x.min())Out[100]: one 2two 3three 8

DataFrameに対して、統計量を求めることが出来る。

Page 16: Intoroduction of Pandas with Python

16

DataFrameに対してソート

In [121]: df.sort_index(by="one")Out[121]: one two threea 1 1 1b 2 2 4c 3 3 9d NaN 4 NaN

In [122]: df.sort_index(by="one",ascending=False)Out[122]: one two threed NaN 4 NaNc 3 3 9b 2 2 4a 1 1 1

ソートも楽に出来る。byに配列を使って複数指定することも可能

Page 17: Intoroduction of Pandas with Python

17

pandasとscikit-learn

参考:http://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.html#example-linear-model-plot-ols-py

gepuro@ubuntu:~$ cat hoge.csv a,b1,12,34,61,32,21,1

Page 18: Intoroduction of Pandas with Python

18

ご清聴ありがとうございました!