A function to create interaction variables: what is wrong with the code?
<p>I’ve written a function below that takes, as arguments, a dataframe (<code>df</code>) and two of its column names (<code>var1</code>, <code>var2</code>).  Then it creates interaction variables for the two variables and adds those columns to the original dataframe.  The code works when I hard code it, but when I try to call the function like:</p>

<pre><code>create_interactions(my_dataframe, 'variable1', 'variable2')
my_dataframe
</code></pre>

<p>I receive no errors but the new columns are not added to the dataframe - it returns the original dataframe.  What am I doing wrong? Thank you.</p>

<pre><code>def create_interactions(df,var1,var2):
    variables = df[[var1,var2]] 
    for i in range(0, variables.columns.size):
        for j in range(0, variables.columns.size):
            col1 = str(variables.columns[i])
            col2 = str(variables.columns[j])
            if i &lt;= j:
                name = col1 + "*" + col2
                df = pd.concat([df, pd.Series(variables[col1] * variables[col2], name=name)], axis=1)
</code></pre>