pandas groupby with lambda expression and dict comprehension
<pre><code>&gt;&gt;&gt; df
   group  valueCol
0      1         1
1      1         2
2      1         3
3      2         4
4      2         5
5      3         6
&gt;&gt;&gt; df.dtypes
group       int64
valueCol    int64
dtype: object
&gt;&gt;&gt; 
</code></pre>

<p>This makes sense:</p>

<pre><code>&gt;&gt;&gt; df.groupby('group')['valueCol'].agg({'mean': np.mean, 'sum': sum, 'len': len})
       sum  mean  len
group                
1        6   2.0    3
2        9   4.5    2
3        6   6.0    1
</code></pre>

<p>This doesn't make sense. I was hoping to see different values for column by adjustment. But it is always the last value from dict-comprehension that is being copied in all columns. Is this expected?</p>

<pre><code>&gt;&gt;&gt; df.groupby('group')['valueCol'].agg({'adjust-by-' + str(diff): lambda x: len(x) + diff for diff in [0, 1, 2]})
       adjust-by-0  adjust-by-1  adjust-by-2
group                                       
1                5            5            5
2                4            4            4
3                3            3            3
&gt;&gt;&gt; df.groupby('group')['valueCol'].agg({'adjust-by-' + str(diff): lambda x: len(x) + diff for diff in [2, 1, 0]})
       adjust-by-0  adjust-by-1  adjust-by-2
group                                       
1                3            3            3
2                2            2            2
3                1            1            1
&gt;&gt;&gt; 
</code></pre>

<p>EDIT:</p>

<p>I was hoping different lambda functions get defined but they are all the same.</p>

<pre><code>&gt;&gt;&gt; functions = {i: lambda x: len(x) + i for i in [0, 1, 2]}
&gt;&gt;&gt; functions
{0: &lt;function &lt;dictcomp&gt;.&lt;lambda&gt; at 0x107447510&gt;, 1: &lt;function &lt;dictcomp&gt;.&lt;lambda&gt; at 0x1074472f0&gt;, 2: &lt;function &lt;dictcomp&gt;.&lt;lambda&gt; at 0x1074471e0&gt;}
&gt;&gt;&gt; df.groupby('group')['valueCol'].agg(lambda x: functions[1](x))
group
1    5
2    4
3    3
Name: valueCol, dtype: int64
&gt;&gt;&gt; df.groupby('group')['valueCol'].agg(lambda x: functions[2](x))
group
1    5
2    4
3    3
Name: valueCol, dtype: int64
</code></pre>

<p>This works. I guess my generator for lambda functions was wrong.</p>

<pre><code>&gt;&gt;&gt; def crtfunc(i):
           return lambda x: len(x) + i

&gt;&gt;&gt; crtfunc(2)
&lt;function crtfunc.&lt;locals&gt;.&lt;lambda&gt; at 0x1074477b8&gt;

&gt;&gt;&gt; crtfunc(2)([1,2,3])
5

&gt;&gt;&gt; functions3 = {i: crtfunc(i) for i in [0, 1, 2]}
&gt;&gt;&gt; functions3
{0: &lt;function crtfunc.&lt;locals&gt;.&lt;lambda&gt; at 0x1074479d8&gt;, 1: &lt;function
crtfunc.&lt;locals&gt;.&lt;lambda&gt; at 0x107447840&gt;, 2: &lt;function
crtfunc.&lt;locals&gt;.&lt;lambda&gt; at 0x107447620&gt;}

&gt;&gt;&gt; df.groupby('group')['valueCol'].agg(functions3[0])
group
1    3
2    2
3    1
Name: valueCol, dtype: int64

&gt;&gt;&gt; df.groupby('group')['valueCol'].agg(functions3[2])
group
1    5
2    4
3    3
Name: valueCol, dtype: int64

&gt;&gt;&gt; df.groupby('group')['valueCol'].agg({'adjust-by'+str(i): functions3[i] for i in [0, 1, 2]})

       adjust-by0  adjust-by2  adjust-by1
group
1               3           5           4
2               2           4           3
3               1           3           2
</code></pre>