<Python, pandas> ix / iloc / loc の違い

パンダさんのデータフレームpandas.DataFrameで、
ixiloclocの違い。

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

書き込める!
素敵!