I have a Pandas data frame that I am trying transpose from long to wide.
Here is the data frame:
Here is the desired output:
Below is reproducible code to make the data frame and also my attempt called test
; I am part way there, but I am not sure how to unmelt again and concatenate the column names.
mydf = pd.DataFrame(
{
"x": [1, 1, 1, 1],
"cat": ["Cat100", "Cat100", "Cat200", "Cat200"],
"n": ["N1", "N2", "N1", "N2"],
"y": [3, 4.2, 2.1, 2.4],
}
)
test = mydf.set_index(["x", "cat", "n"])["y"].unstack().reset_index()
test.columns = test.columns.tolist()
display(test)