create df column name by adding global variable name and a string in Python
<p>I have a global variable 
<code>
split_ask_min = 'Minimum_Spend'
</code></p>

<p>I would like to create a new variable in my df and name it 'Minimum_Spend_Sum' and make it the sum of Minimum_Spend. </p>

<p><code>
var_programs['split_ask_min+ _Sum'] = var_programs[split_ask_min].groupby(X['NAME']).transform('sum')
</code>
I am having trouble creating the variable name. It should be </p>

<p><code>
split_ask_min+ '_Sum'
</code> 
which equal to 
<code>
Minimum_Spend_Sum
</code></p>

<p>But then if I code </p>

<p><code>var_programs['split_ask_min+ '_Sum'']
</code></p>

<p>I got an error</p>

<p>To create a new column in your df you can pass a constructed string to add a new column to your df:</p>

<pre><code>In [239]:
split_ask_min = 'Minimum_Spend'
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))
df[split_ask_min + '_Sum'] = 0
df

Out[239]:
          a         b         c  Minimum_Spend_Sum
0 -0.113483 -0.487551  0.276176                  0
1  0.143454 -1.322578 -2.040297                  0
2 -0.100320  0.716255  1.109432                  0
3 -1.260516 -0.560957  0.007871                  0
4  0.497407 -2.031782  0.933199                  0
</code></pre>

python python-3.x pandas