Keeping 'key' column when using groupby with transform in pandas
Another way to achieve something similiar to what Pepacz suggested:
df.loc[:, df.columns.drop('a')] = df.groupby('a').transform(lambda x: x)
that is bizzare!
I tricked it like this
df.groupby(df.a.values).transform(lambda x: x)
The issue is discussed also here.
The returned object has the same indices as the original df, therefore you can do
pd.concat([
df['a'],
df.groupby('a').transform(lambda x: x)
], axis=1)