0

I have the initial table below:

initial table

And I want to achieve this format:

final format

I managed to get the requested shape for one "val" at once. But it's not very useful in my case, as every table contains many values and the final result should be 1 consolidated table:

d={"name":["A","B","C","A","B","C"],"category":["1","1","1","2","2","2"],"val1":[5.5,2.3,4.2,3.5,3,3.7],"val2":[2.2,2.2,2.2,1.1,1.1,1.1
],"val3":[7.1,6.2,7.4,0.2,4,6.2]}

df=pd.DataFrame(d)
pd.crosstab(index=df["name"],columns=df["category"],values=df["val1"],aggfunc="mean",colnames=["val1"])

`

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74

0 Answers0