Error iterating through a Pandas series
<p>When I get the first and second elements of this series, it works OK, but from element 3 onwards, giving an error when I try to fetch.</p>

<pre><code>type(X_test_raw)
Out[51]: pandas.core.series.Series

len(X_test_raw)
Out[52]: 1393

X_test_raw[0]
Out[45]: 'Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...'

X_test_raw[1]
Out[46]: 'Ok lar... Joking wif u oni...'

X_test_raw[2]
</code></pre>

<blockquote>
  <p>KeyError: 2</p>
</blockquote>

<p>consider the series <code>X_test_raw</code></p>

<pre><code>X_test_raw = pd.Series(
    ['Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...',
     'Ok lar... Joking wif u oni...',
     'PLEASE DON\'T FAIL'
    ], [0, 1, 3])
</code></pre>

<p><code>X_test_raw</code> doesn't have an index of <code>2</code> which you are trying to reference with <code>X_test_raw[2]</code>.</p>

<p>Instead use <code>iloc</code></p>

<pre><code>X_test_raw.iloc[2]

"PLEASE DON'T FAIL"
</code></pre>

<p>You can iterate through the series with <code>iteritems</code></p>

<pre><code>for index_val, series_val in X_test_raw.iteritems():
    print series_val

Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...
Ok lar... Joking wif u oni...
PLEASE DON'T FAIL
</code></pre>

python pandas for-loop indexing keyerror