How to sum and to mean one DataFrame to create another DataFrame
<p>After creating DataFrame with some duplicated cell values in the column <strong>Name</strong>:</p>

<pre><code>import pandas as pd
df = pd.DataFrame({'Name': ['Will','John','John','John','Alex'],
                   'Payment':  [15, 10, 10, 10, 15],
                   'Duration':    [30, 15, 15, 15, 20]})
</code></pre>

<p><a href="http://i.stack.imgur.com/MEFnO.png"><img src="http://i.stack.imgur.com/MEFnO.png" alt="enter image description here"></a></p>

<p>I would like to proceed by creating another DataFrame where the duplicated values in <strong>Name</strong> column are consolidated leaving no duplicates. At the same time I want 
to sum the payments values John made. I proceed with:</p>

<pre><code>df_sum = df.groupby('Name', axis=0).sum().reset_index()
</code></pre>

<p><a href="http://i.stack.imgur.com/7i89K.png"><img src="http://i.stack.imgur.com/7i89K.png" alt="enter image description here"></a></p>

<p>But since <code>df.groupby('Name', axis=0).sum()</code> command applies the sum function to every column in DataFrame the <strong>Duration</strong> (of the visit in minutes) column is processed as well. Instead I would like to get an average values for the <strong>Duration</strong> column. So I would need to use <code>mean()</code> method, like so:</p>

<pre><code>df_mean = df.groupby('Name', axis=0).mean().reset_index()
</code></pre>

<p><a href="http://i.stack.imgur.com/iojOc.png"><img src="http://i.stack.imgur.com/iojOc.png" alt="enter image description here"></a></p>

<p>But with <code>mean()</code> function the column <strong>Payment</strong> is now showing the average payment values John made and not the sum of all the payments. </p>

<p>How to create a DataFrame where Duration values show the average values while the Payment values show the sum?</p>

<p>You can apply different functions to different columns with groupby.agg:</p>

<pre><code>df.groupby('Name').agg({'Duration': 'mean', 'Payment': 'sum'})
Out: 
      Payment  Duration
Name                   
Alex       15        20
John       30        15
Will       15        30
</code></pre>

python pandas dataframe