Using:
df=pd.DataFrame([['group_A','buy',100,123],['group_A','view',0,111],['group_B','view',0,222],['group_A','view',0,222]],columns=['group','type','amount','number'])
First we summarize the indices and orient:
>>> df = df.groupby(['type','group']).sum().transpose().stack(0).reset_index() >>> df group level_0 type group_A group_B 0 amount buy 100 NaN 1 amount view 0 0 2 number buy 123 NaN 3 number view 333 222
Discard all null lines:
df = df[~((df['group_A']==0) | (df['group_B']==0))]
Fillna's:
>>> df.fillna(0) group level_0 type group_A group_B 0 amount buy 100 0 2 number buy 123 0 3 number view 333 222
Some guessing in several places here, but this should give you a start.
Daniel
source share