<Python, pandas> ix / iloc / loc の違い
パンダさんのデータフレームpandas.DataFrame
で、
ix
とiloc
とloc
の違い。
DataFrame
が空empty
の場合、loc
さんだけは、データを追加できる!
例。
まずは、空のDataFrame
作成。
In [63]: import pandas as pd In [64]: df = pd.DataFrame() In [65]: df Out[65]: Empty DataFrame Columns: [] Index: []
ix
だと、
In [66]: df.ix[0, 0] = 'hage' --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-66-8c64195fdb6e> in <module>() ----> 1 df.ix[0, 0] = 'hage' : ValueError: cannot set by positional indexing with enlargement
ゲロー。
で、iloc
だと、
In [67]: df.iloc[0, 0] = 'hage' --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-67-2dc8f26b1537> in <module>() ----> 1 df.iloc[0, 0] = 'hage' : IndexError: single positional indexer is out-of-bounds
ゲロー。
で、loc
だと、
In [68]: df.loc[0, 0] = 'hage' In [69]: df Out[69]: 0 0 hage
書き込める!
素敵!