Python Pandas Data Formatting: Diffnce in passing index and iloc
<p>This is the sample data:</p>

<pre><code>dict_country_gdp = pd.Series([52056.01781,40258.80862,40034.85063,39578.07441],
    index = ['Luxembourg','Norway', 'Japan', 'Switzerland'])
</code></pre>

<p>What is the difference between <code>dict_country_gdp[0]</code> and <code>dict_country_gdp.iloc[0]</code>? </p>

<p>While the result is the same, when to use which?</p>

<p>As you are working with one dimensional series, [] or .iloc will give same results.  </p>

<p><strong>ONE DIMENSIONAL SERIES:</strong></p>

<pre><code>import pandas as pd

dict_country_gdp = pd.Series([52056.01781, 40258.80862,40034.85063,39578.07441])

dict_country_gdp

Out[]: 
0    52056.01781
1    40258.80862
2    40034.85063
3    39578.07441
dtype: float64

dict_country_gdp[0]
Out[]: 52056.017809999998

dict_country_gdp.iloc[0]
Out[]: 52056.017809999998   
</code></pre>

<p><strong>MULTI-DIMENSIONAL SERIES:</strong>                      </p>

<pre><code>dict_country_gdp = pd.Series([52056.01781, 40258.80862,40034.85063,39578.07441],[52056.01781, 40258.80862,40034.85063,39578.07441])

dict_country_gdp 
Out[]: 
52056.01781    52056.01781
40258.80862    40258.80862
40034.85063    40034.85063
39578.07441    39578.07441
dtype: float64
</code></pre>

<p>Now in this scenario, you cannot access series using [] operator. </p>

<pre><code>dict_country_gdp[0]
Out[]: KeyError: 0.0

dict_country_gdp.iloc[0]
Out[]: 52056.017809999998 
</code></pre>

<p>iloc provides more control while accessing multidimensional series:</p>

<pre><code>dict_country_gdp[0:2]
Out[]: Series([], dtype: float64)

dict_country_gdp.iloc[0:2]
Out[]: 
52056.01781    52056.01781
40258.80862    40258.80862
dtype: float64
</code></pre>

<p><a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-integer" rel="nofollow">Documentation</a> states:</p>

<p>.iloc is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array. .iloc will raise IndexError if a requested indexer is out-of-bounds, except slice indexers which allow out-of-bounds indexing. (this conforms with python/numpy slice semantics). Allowed inputs are:</p>

<ul>
<li>An integer e.g. 5</li>
<li>A list or array of integers [4, 3, 0]</li>
<li>A slice object with ints 1:7</li>
<li>A boolean array</li>
<li>A callable function with one argument (the calling <strong>Series, DataFrame
or Panel</strong>) and that returns valid output for indexing (one of the
above)</li>
</ul>

<p>This is why one cannot use [] operator with dataframe objects. Only iloc can be used when it comes to dataframes and multidimensional series.  </p>

python pandas dictionary