簡體   English   中英

Pandas pivot 表到 dataframe

[英]Pandas pivot table to dataframe

我有一個 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 |
+---------+------------+-------+----+

它來自使用以下代碼的 dataframe (df):

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

其中 ZY 字段有兩個類 z 和 y。

我嘗試使用

df = table.reset_index()

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

我想要一個像這樣的 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 |
+---------+-------+------------+----------+

這有點晚了,但我認為擺脫pt.columns.name (即"ZY" )並重置索引將返回預期的 output。 方法鏈( 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()

更直接的方法是reset_index()並明確刪除columns.name

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

一個可重現的例子:

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