pandasの使い方(DataFrame編)
pandasのDataFrameの使い方のメモです。
pandas
pandasの説明とインストール方法は下記を参照。
pppurple.hatenablog.com
DataFrame
DataFrameは行と列のテーブル形式のデータ構造を持った、2次元のオブジェクトです。
IPythonで使ってみます。
SeriesとDataFrameを直接名前指定で使えるようにインポートして使います。
pandasはpdという省略名で使うのが通例です。
In [108]: from pandas import Series, DataFrame In [109]: import pandas as pd
同じ長さの配列を持ったディクショナリを引数にして生成。
In [6]: data = {'Fruits' : ['Orange', 'Orange', 'Orange', 'Apple', 'Banana'], 'shop' : ['AAA', 'BBB', 'CCC', 'AAA', 'BBB'], 'price' : [100, 150, 200, 300, 250]} In [7]: frame = DataFrame(data)
インデックスは自動で生成される。
In [8]: frame Out[8]: Fruits price shop 0 Orange 100 AAA 1 Orange 150 BBB 2 Orange 200 CCC 3 Apple 300 AAA 4 Banana 250 BBB
columnsで列の順序を指定できる。
In [10]: DataFrame(data, columns=['price', 'shop', 'Fruits']) Out[10]: price shop Fruits 0 100 AAA Orange 1 150 BBB Orange 2 200 CCC Orange 3 300 AAA Apple 4 250 BBB Banana
columnsで存在しない列が指定された場合、NaNが割り当てられる。
In [11]: DataFrame(data, columns=['price', 'shop', 'Fruits', 'color']) Out[11]: price shop Fruits color 0 100 AAA Orange NaN 1 150 BBB Orange NaN 2 200 CCC Orange NaN 3 300 AAA Apple NaN 4 250 BBB Banana NaN
indexの配列を指定するとインデックス名を指定できる。
In [12]: DataFrame(data, index=['one', 'two', 'three', 'four', 'five']) Out[12]: Fruits price shop one Orange 100 AAA two Orange 150 BBB three Orange 200 CCC four Apple 300 AAA five Banana 250 BBB
columnsフィールドで行のインデックスオブジェクトを参照できる。
In [13]: frame2 = DataFrame(data, columns=['price', 'shop', 'Fruits', 'color'], index=['one', 'two', 'three', 'four', 'five']) In [14]: frame2 Out[14]: price shop Fruits color one 100 AAA Orange NaN two 150 BBB Orange NaN three 200 CCC Orange NaN four 300 AAA Apple NaN five 250 BBB Banana NaN In [15]: frame2.columns Out[15]: Index(['price', 'shop', 'Fruits', 'color'], dtype='object')
列の参照。
In [16]: frame2['shop'] Out[16]: one AAA two BBB three CCC four AAA five BBB Name: shop, dtype: object In [18]: frame2.shop Out[18]: one AAA two BBB three CCC four AAA five BBB Name: shop, dtype: object
行を参照する場合、ixというフィールドを使って参照できる。
In [19]: frame2.ix['two'] Out[19]: price 150 shop BBB Fruits Orange color NaN Name: two, dtype: object
列の値を更新。列名指定でスカラー値を代入するとすべての値が更新される。
In [20]: frame2.color = 'red' In [21]: frame2 Out[21]: price shop Fruits color one 100 AAA Orange red two 150 BBB Orange red three 200 CCC Orange red four 300 AAA Apple red five 250 BBB Banana red
列の値を更新。列名指定で配列を代入すると各値が更新される。
配列の長さはDataFrameの長さと一致している必要がある。
In [28]: c = ['orange', 'orange', 'orange', 'red', 'yellow'] In [29]: frame2.color = c In [30]: frame2 Out[30]: price shop Fruits color one 100 AAA Orange orange two 150 BBB Orange orange three 200 CCC Orange orange four 300 AAA Apple red five 250 BBB Banana yellow
列の削除
In [32]: del frame2['color'] In [33]: frame2 Out[33]: price shop Fruits one 100 AAA Orange two 150 BBB Orange three 200 CCC Orange four 300 AAA Apple five 250 BBB Banana
ネストしたディクショナリからDataFrameを生成。
一番外側のキーが列のインデックスになり、
内側のキーが行のインデックスになる。
In [35]: data = {'Orange': {'AAA': 100, 'BBB': 150}, 'Apple': {'AAA': 200, 'BBB': 220, 'CCC': 300}} In [36]: frame3 = DataFrame(data) In [37]: frame3 Out[37]: Apple Orange AAA 200 100 BBB 220 150 CCC 300 NaN
DataFrameの転置を取ることで、形式を変更できる。
In [38]: frame3.T Out[38]: AAA BBB CCC Apple 200 220 300 Orange 100 150 NaN
indexの配列を指定した場合は、そちらが優先される。
In [39]: DataFrame(data, index=['BBB', 'CCC', 'DDD']) Out[39]: Apple Orange BBB 220 150 CCC 300 NaN DDD NaN NaN
index.name属性でインデックス名を指定。
In [40]: frame3.index.name = 'shop' In [41]: frame3 Out[41]: Apple Orange shop AAA 200 100 BBB 220 150 CCC 300 NaN
columns.name属性で列名を指定。
In [43]: frame3.columns.name = 'Fruits' In [44]: frame3 Out[44]: Fruits Apple Orange shop AAA 200 100 BBB 220 150 CCC 300 NaN
values属性を参照すると、データのみが2次元ndarrayとして参照できる。
In [45]: frame3.values Out[45]: array([[ 200., 100.], [ 220., 150.], [ 300., nan]]) In [46]: frame2.values Out[46]: array([[100, 'AAA', 'Orange'], [150, 'BBB', 'Orange'], [200, 'CCC', 'Orange'], [300, 'AAA', 'Apple'], [250, 'BBB', 'Banana']], dtype=object)
DataFrameのreindex()で再インデックス付けが可能。
列インデックスの再インデックス。
In [70]: frame = DataFrame(np.arange(9).reshape((3, 3)), index=['a', 'b', 'c'], ....: columns=['AAA', 'BBB', 'CCC']) In [71]: frame Out[71]: AAA BBB CCC a 0 1 2 b 3 4 5 c 6 7 8 In [72]: frame2 = frame.reindex(['a', 'b', 'c', 'd']) In [73]: frame2 Out[73]: AAA BBB CCC a 0 1 2 b 3 4 5 c 6 7 8 d NaN NaN NaN
columnsを指定すると行インデックスの再インデックスが可能。
In [75]: col = ['BBB', 'CCC', 'DDD'] In [76]: frame.reindex(columns=col) Out[76]: BBB CCC DDD a 1 2 NaN b 4 5 NaN c 7 8 NaN
インデックスオブジェクトはsetの様に機能する。
In [56]: frame3 Out[56]: Fruits Apple Orange shop AAA 200 100 BBB 220 150 CCC 300 NaN In [57]: 'Apple' in frame3.columns Out[57]: True In [58]: 'AAA' in frame3.index Out[58]: True In [59]: 'DDD' in frame3.index Out[59]: False
終わり。