Iterating over Dataframe
<p>I'm trying to iterate over a dataframe where the "Filename" column is consisted of strings. I have the following, however, I get the following Error.</p>

<p>result is a different Dataframe</p>

<pre><code>k = 1
l = 0
for row in df.iterrows():
    if k % 3 == 0:
        result.loc[l, 'H2'] = row['Filename']
        l += 1
    elif k % 2 == 0:
        result.loc[l, 'H1'] = row['Filename']
    else:
        result.loc[l, 'V'] = row['Filename']
    k += 1
</code></pre>

<p>The error I get is:</p>

<pre><code>TypeError: tuple indices must be integers or slices, not str
</code></pre>

<p>Any idea of a fix?</p>

<p>when you iter through df with <code>df.iterrows()</code> it will return a tuple for each row where row[0] is the index of the row and row[1] is a Series.</p>

<p>so you could do:</p>

<pre><code>row[1]['Filename']
</code></pre>

<p>personally I like to iter using <code>.itertuples()</code> which returns named tuple :</p>

<pre><code>for row in df2.itertuples():
    print row.Filename
</code></pre>

python pandas dataframe