How can I apply a list function to a grouped pandas dataframe which is not aggregated using apply but rather transform?
For me the following fails with: KeyError: "None of [Index(['v1', 'v2'], dtype='object')] are in the [index]"
import pandas as pd
df = pd.DataFrame({'key':[1,1,1,2,3,2], 'v1': [1,4,6,7,4,9], 'v2':[0.3, 0.6, 0.4, .1, .2, .8]})
display(df)
def list_function(x):
#display(x)
all_values = x[['v1','v2']].drop_duplicates()
#display(all_values)
#result = all_values.to_json()
result = all_values.values
return result
display(df.groupby(['key']).apply(list_function))
df['list_result'] = df.groupby(['key']).transform(list_function)
df
NOTICE: I know that a join would be possible with the aggregated data, but in this particular case I would prefer not having to do the JOIN afterwards.