despite the extensive help provided here and here I was unable to figure out how to do the following:
given this dataset (df):
import pandas as pd
improt numpy as np
df = pd.DataFrame([['CORE1', 'CORE2', 'CORE3', 'CORE1', 'CORE2', 'CORE3', 'CORE1', 'CORE2', 'CORE3', ],
['alfa', 'beta', 'gamma', 'alfa', 'beta', 'gamma', 'alfa', 'beta', 'gamma', ],
np.random.rand(9).tolist()],
index=['ptf', 'name', 'value']).transpose()
name value ptf
alfa 0.1 CORE1
beta 0.7 CORE1
gamma 0.2 CORE1
alfa 0.3 CORE2
beta 0.4 CORE2
gamma 0.3 CORE2
alfa 0.9 CORE3
beta 0.05 CORE3
gamma 0.05 CORE3
turn into
CORE1 CORE2 CORE3
alfa 0.1 0.3 0.9
beta 0.7 0.4 0.05
gamma 0.2 0.3 0.05
I was guessing somewhere in the lines of df.groupby(by='ptf')
and something after. what exactly reamins to be understood.
Edit:
print(df.dtypes)
# 1st - works but takes numerate index - not what I want
print(df.pivot(columns='ptf', values='value'))
# 2nd - textbook made - does not work
print(df.pivot(index='name', columns='ptf', values='value'))
# 3rd - same as 2nd but with different constructor
print(pd.pivot_table(df, index='name', values='value', columns='ptf'))
Any help in the matter?