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>There is no index with value <code>2</code>.</p>

<p>Sample:</p>

<pre><code>X_test_raw = pd.Series([4,8,9], index=[0,4,5])

print (X_test_raw)
0    4
4    8
5    9
dtype: int64

#print (X_test_raw[2])
#KeyError: 2
</code></pre>

<p>If need third value use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.iloc.html" rel="nofollow"><code>iloc</code></a>:</p>

<pre><code>print (X_test_raw.iloc[2])
9
</code></pre>

<p>If need iterating only values:</p>

<pre><code>for x in X_test_raw:
    print (x)
4
8
9
</code></pre>

<p>If need <code>indexes</code> and <code>values</code> use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.iteritems.html" rel="nofollow"><code>Series.iteritems</code></a>:</p>

<pre><code>for idx, x in X_test_raw.iteritems():
    print (idx, x)
0 4
4 8
5 9
</code></pre>

python pandas for-loop indexing keyerror