pandas dataframe: len(df) is not equal to number of iterations in df.iterrows()
<p>I have a dataframe where I want to print each row to a different file. When the dataframe consists of e.g. only 50 rows, <code>len(df)</code> will print <code>50</code> and iterating over the rows of the dataframe like</p>

<pre><code>for index, row in df.iterrows():
    print(index)
</code></pre>

<p>will print the index from <code>0</code> to <code>49</code>. </p>

<p>However, if my dataframe contains more than 50'000 rows, <code>len(df)</code>and the number of iterations when iterating over <code>df.iterrows()</code> differ significantly. For example, <code>len(df)</code> will say e.g. 50'554 and printing the index will go up to over 400'000. </p>

<p>How can this be? What am I missing here?</p>

<p>First, as @EdChum noted in the comment, your question's title refers to <code>iterrows</code>, but the example you give refers to <a href="http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.iteritems.html" rel="nofollow"><code>iteritems</code></a>, which loops in the orthogonal direction to that relevant to <code>len</code>. I assume you meant <code>iterrows</code> (as in the title). </p>

<p>Note that a DataFrame's index need not be a running index, irrespective of the size of the DataFrame. For example:</p>

<pre><code>df = pd.DataFrame({'a': [1, 2, 3, 4]}, index=[2, 4, 5, 1000])

&gt;&gt;&gt; for index, row in df.iterrows():
...     print index
2
4
5
1000
</code></pre>

<p>Presumably, your long DataFrame was just created differently, then, or underwent some manipulation, affecting the index.</p>

<p>If you <em>really</em> must iterate with a running index, you can use Python's <a href="https://docs.python.org/2/library/functions.html#enumerate" rel="nofollow"><code>enumerate</code></a>:</p>

<pre><code>&gt;&gt;&gt; for index, row in enumerate(df.iterrows()):
...     print index
0
1
2
3
</code></pre>

<p>(Note that, in this case, <code>row</code> is itself a tuple.)</p>

python pandas dataframe