i have data frame and used the command pd.pivot_table(df,columns="category",index=["year","period"])
did some data processing at this dataframe and i want to reverse the process to get the original formation of the df.
i tried with pd.melt
and pd.wide_to_long
without any luck. also on the pivot_table the the columns are a combination of two values, something like that:
("a","1"),("a","2"),("a","3"), .... ,("d","5") where the 1st position are columns from the original dataframe and the 2nd values of the category column
original df cols:
year period category a b c d
1
2
3
4
5
pivot_table:
|a | b | ... | d
category |1 2 3 4 5 | 1 2 3 4 5 | ... | 1 2 3 4 5
year | period
>
data={"col1":[1111,1111,1111,1111,2222,2222,2222,2222],
"col2":["a1","a1","a1","a1","a2","a2","a2","a2"],
"col3":[1,2,1,2,1,2,1,2],"
"a":[555,555,555,555,555,555,555,555],
"b":[666,666,666,666,666,666,666,666]}
df=pd.DataFrame.from_dict(data)
table=pd.pivot_table(df,columns="col3",index=["col1","col2"])