How to iterate over pandas dataframe and create new column
<p>I have a pandas dataframe that has 2 columns. I want to loop through it's rows and based on a string from column 2 I would like to add a string in a newly created 3th column. I tried:</p>

<pre><code>for i in df.index:
    if df.ix[i]['Column2']==variable1:
        df['Column3'] = variable2
    elif df.ix[i]['Column2']==variable3:
        df['Column3'] = variable4

print(df)
</code></pre>

<p>But the resulting dataframe has in column 3 only Variable2.</p>

<p>Any ideas how else I could do this?</p>

<p>You can also try this (if you want to keep the <code>for</code> loop you use) :</p>

<pre><code>new_column = []

for i in df.index:
    if df.ix[i]['Column2']==variable1:
        new_column.append(variable2)
    elif df.ix[i]['Column2']==variable3:
        new_column.append(variable4)
    else : #if both conditions not verified
        new_column.append(other_variable)

df['Column3'] = new_column
</code></pre>

python loops pandas condition multiple-columns