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>The simple fix to your problem is to unpack the tuple coming from <code>iterrows</code></p>

<pre><code>k = 1
l = 0
for i, 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>

<hr>

<p>An improvement on this would be to use <code>enumerate</code> to capture <code>k</code> without having to track it yourself.</p>

<pre><code>l = 0
for k, (i, row) in enumerate(df.iterrows(), 1):
    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']
</code></pre>

<hr>

<p>However, I'm not quite sure what you're doing.  If by chance you're trying to capture every 3rd elements starting with the first, second, and third entries, then you aren't accomplishing that.  If you're sure of this logic then disregard the following suggestion.</p>

<pre><code>pd.concat([df.Filename.iloc[0::3].reset_index(drop=True),
           df.Filename.iloc[1::3].reset_index(drop=True),
           df.Filename.iloc[2::3].reset_index(drop=True)],
          axis=1, keys=['V', 'H1', 'H2'])
</code></pre>

<p>Or</p>

<pre><code>df.Filename.groupby(np.arange(df.shape[0]) % 3) \
    .apply(pd.Series.reset_index, drop=True) \
    .unstack(0).rename(columns={0: 'V', 1: 'H1', 2: 'H2'})
</code></pre>

python pandas dataframe