Keeping 'key' column when using groupby with transform in pandas
<p>Finding a normalized dataframe removes the column being used to group by, so that it can't be used in subsequent groupby operations.  for example (edit: updated):</p>

<pre><code>    df = pd.DataFrame({'a':[1, 1 , 2, 3, 2, 3], 'b':[0, 1, 2, 3, 4, 5]})

       a  b
    0  1  0
    1  1  1
    2  2  2
    3  3  3
    4  2  4
    5  3  5

    df.groupby('a').transform(lambda x: x)

       b
    0  0
    1  1
    2  2
    3  3
    4  4
    5  5
</code></pre>

<p>Now, with most operations on groups the 'missing' column becomes a new index (which can then be adjusted using <code>reset_index</code>, or set <code>as_index=False</code>), but when using transform it just disappears, leaving the original index and a new dataset without the key. </p>

<p>Edit: here's a one liner of what I would like to be able to do</p>

<pre><code>    df.groupby('a').transform(lambda x: x+1).groupby('a').mean()
    KeyError 'a'
</code></pre>

<p>In the example from the <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html" rel="nofollow">pandas docs</a> a function is used to split based on the index, which appears to avoid this issue entirely.  Alternatively, it would always be possible just to add the column after the groupby/transform, but surely there's a better way?</p>

<p>Update:
It looks like reset_index/as_index are intended only for functions that reduce each group to a single row.  There seem to be a couple options, from answers</p>

<p>that is bizzare!</p>

<p>I tricked it like this</p>

<pre><code>df.groupby(df.a.values).transform(lambda x: x)
</code></pre>

<p><a href="https://i.stack.imgur.com/XcYxq.png" rel="nofollow"><img src="https://i.stack.imgur.com/XcYxq.png" alt="enter image description here"></a></p>

python pandas