I have a pivot table (pt) that looks like this:
+---------+------------+-------+----+
| | ZY | z | y |
+---------+------------+-------+----+
| period_s| ZONE | | |
+---------+------------+-------+----+
| 201901 | A | 14 | 34 |
| | B | 232 | 9 |
| | C | 12 | 2 |
+---------+------------+-------+----+
| 201902 | A | 196 | 70 |
| | K | 10 | 1 |
| | D | 313 | 99 |
+---------+------------+-------+----+
which came from a dataframe (df) using the following code:
pt=df.pivot_table(index=['period_s','ZONE'], columns='ZY', values='ID', aggfunc="count")
where the ZY field has two classes z and y.
I tried using the
df = table.reset_index()
also
df.columns = df.columns.droplevel(0) #remove amount
df.columns.name = None #remove categories
df = df.reset_index()
As mentioned here transform pandas pivot table to regular dataframe and like this one Convert pivot tables to dataframe
I want to have a dataframe like this:
+---------+-------+------------+----------+
| period_s| ZONE | z | y |
+---------+-------+------------+----------+
| 201901 | A | 14 | 34 |
| 201901 | B | 232 | 9 |
| 201901 | C | 12 | 2 |
| 201902 | A | 196 | 70 |
| 201902 | K | 10 | 1 |
| 201902 | D | 313 | 99 |
+---------+-------+------------+----------+