Pandas Group By and Count
<p>A pandas dataframe df has 3 columns: </p>

<p>user_id,
session,
revenue</p>

<p>What I want to do now is group df by unique user_id and derive 2 new columns - one called number_sessions (counts the number of sessions associated with a particular user_id) and another called number_transactions (counts the number of rows under the revenue column that has a value > 0 for each user_id). How do I go about doing this?</p>

<p>I tried doing something like this: </p>

<pre><code>df.groupby('user_id')['session', 'revenue'].agg({'number sessions': lambda x: len(x.session), 
'number_transactions': lambda x: len(x[x.revenue&gt;0])})
</code></pre>