How to create new calculated columns in pandas and remove original?
<p>I have a pandas dataframe 'df' that has:</p>

<pre><code>name a b
greg 1 1
george 2 2
giles 3 3
giovanni 4 5
</code></pre>

<p>I want to run this dataframe through a calcuate function to create new columns: c and d such that I get the following resulting dataframe:</p>

<pre><code>name c d
greg 11 21
george 12 22
giles 13 23
giovanni 14 24
</code></pre>

<p>Currently, my code is as follows:</p>

<p>My calcluate function:</p>

<pre><code>def calculate(row):
   return row['a']+10, row['b']+20
</code></pre>

<p>My function to modify the dataframe:</p>

<pre><code>df['c'] = df.apply(calculate, axis=1)
</code></pre>

<p>The resulting dataframe I am getting is this:</p>

<pre><code>name a b c
greg 1 1 (11, 21)
george 2 2(12, 22)
giles 3 3 (13, 23)
giovanni 4 4 (14, 24)
</code></pre>

<p>How do I get my dataframe to look like: </p>

<pre><code>name c d
greg 11 21
george 12 22
giles 13 23
giovanni 14 24
</code></pre>

<p>Row iteration is very slow.  You are much better off doing something like the following:</p>

<pre><code>df['c'] = df.a + 10
df['d'] = df.b + 20
df.drop(['a', 'b'], axis='columns', inplace=True)
</code></pre>

<p>To implement your method, however, you would need to do this:</p>

<pre><code>df['c'], df['d'] = zip(*df.apply(calculate, axis=1))
&gt;&gt;&gt; df
       name  a  b   c   d
0      greg  1  1  11  21
1    george  2  2  12  22
2     giles  3  3  13  23
3  giovanni  4  5  14  25
</code></pre>

python pandas