Let's suppose we have such dataframe:
df = pd.DataFrame({'key' : ['one', 'two', 'three', 'four'] * 3,
'col' : ['A', 'B', 'C'] * 4,
'val1' : np.random.randn(12),
'val2' : np.random.randn(12),
'val3' : np.random.randn(12)})
key + col
is unique key
I want to make col
values to become columns split or to cross-tabulate on them and finally to looks something like this:
First naive approach pd.crosstab(df.key,df.col)
didn't work here well:
This code pd.crosstab(df.key,df.col,values = df[['val1', 'val2', 'val3']], aggfunc = np.max)
failed to run with ValueError: Wrong number of items passed 3, placement implies 1
How get it work?