I am trying to split a column containing a list of values into multiple columns after groupby. I want to do this on the fly by adding columns based on index position rather than creating a new separate Dataframe.
The below set of code generates data and creates a groupby object and a column list using the custom function fnpercentiles
import pandas as pd
#DATA
df = pd.DataFrame(data=
{'ticker': ['AAPL','AAPL','AAPL','IBM','IBM','IBM'],
'side': ['B','B','S','S','S','B'],
'size': [100, 200, 300, 400, 100, 200],
'price': [10.12, 10.13, 10.14, 20.3, 20.2, 20.1]})
#FUNCTION
def fnpercentiles(a):
return [np.percentile(a, 0.25), np.percentile(a, 0.75)]
g = df.groupby(['ticker', 'side'])
#OPERATION
g12=pd.DataFrame()
g12['price/mean'] = g['size'].mean()/g['price'].sum()
g12['fn-cust'] = g['price'].agg([fnpercentiles])
I can generate a new Dataframe by splitting columns (see code below)
h12 = pd.DataFrame(g12['fn-cust'].tolist())
But I want to insert individual columns from the list directly into the Dataframe that's already being generated. I tried the below code and a few variants to no avail
#doesn't work
g12['list_col1'] = g['price'].agg([fnpercentiles]).tolist()[0]
A workaround would be to first split the list into a new Dataframe and insert other columns later. But is there a way to achieve I want without this hack?