This is my df
:
id val1 val2 cnt
1 5 6 1
2 2 5 2
2 5 1 1
3 4 2 1
3 1 3 2
I run this code and get MultiIndex:
df = df.pivot(index="id",
columns="cnt",
values=["val1","val2"]) \
.fillna(0)
If I add .reset_index(level=["id"])
, then I get columnswith (
, e.g. ('id', '')
.
How can I fix it?
If need flatten
MultiIndex
in columns usef-string
s in list comprehension:Another solution:
output