简体   繁体   English

Pandas pivot 表到 dataframe

[英]Pandas pivot table to dataframe

I have a pivot table (pt) that looks like this:我有一个 pivot 表(pt),如下所示:

+---------+------------+-------+----+
|         | 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:它来自使用以下代码的 dataframe (df):

pt=df.pivot_table(index=['period_s','ZONE'], columns='ZY', values='ID', aggfunc="count")

where the ZY field has two classes z and y.其中 ZY 字段有两个类 z 和 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 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:我想要一个像这样的 dataframe:

+---------+-------+------------+----------+
| 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 |
+---------+-------+------------+----------+

It's a bit late but I think getting rid of pt.columns.name (ie "ZY" ) and resetting index would return the expected output.这有点晚了,但我认为摆脱pt.columns.name (即"ZY" )并重置索引将返回预期的 output。 A method chain ( set_axis() or rename_axis() to get rid of columns.name and reset_index() to convert period_s and ZONE into columns).方法链( set_axis()rename_axis()摆脱columns.namereset_index()period_sZONE转换为列)。

pt.set_axis(pt.columns.tolist(), axis=1).reset_index()
#pt.rename_axis(None, axis=1).reset_index()

A more straightforward way is to reset_index() and remove columns.name explicitly.更直接的方法是reset_index()并明确删除columns.name

pt.reset_index(inplace=True)
pt.columns.name = None

A reproducible example:一个可重现的例子:

import numpy as np
df = pd.DataFrame({'period_s': np.random.choice([201901, 201902], size=100),
                   'ZONE': np.random.choice([*'ABC'], size=100),
                   'ZY': np.random.choice([*'zy'], size=100),
                   'ID': np.arange(100)})
pt = df.pivot_table(index=['period_s','ZONE'], columns='ZY', values='ID', aggfunc="count")

# output
pt.set_axis(pt.columns.tolist(), axis=1).reset_index()

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM