I have a melted, long-format dataframe like this:
name = ["A", "A", "B", "B"]
varA = [1, 2, 1, 2]
varB = [200, 250, 200, 250]
val = [4, 8, 1, 0]
df = pd.DataFrame(
data=zip(name, varA, varB, val), columns=["name", "varA", "varB", "val"]
)
How can I reshape it like this?
There is a similar question here, so I tried the following, which threw the error ValueError: Index contains duplicate entries, cannot reshape
df2 = (df.set_index(['varA','varB'])
.stack()
.unstack(0)
.reset_index()
.rename_axis(None, axis=1))
I'm sure this is easy for someone familiar with all the Pandas functions/methods, but there are a ton of functions for the casual user to keep track of!