簡體   English   中英

如何將pandas數據透視表轉換為數據幀

[英]How do I convert a pandas pivot table to a dataframe

我想使用數據透視表來匯總數據集,然后能夠訪問數據透視表中的信息,就像它是一個DataFrame一樣。

考慮一個分層數據集,患者在醫院和位於地區的醫院接受治療:

import pandas as pd

example_data = {'patient' : ['p1','p2','p3','p4','p5','p6','p7','p8','p9','p10','p11','p12','p13','p14','p15','p16','p17','p18','p19','p20','p21','p22','p23','p24','p25','p26','p27','p28','p29','p30','p31','p32','p33','p34','p35','p36','p37','p38','p39','p40','p41','p42','p43','p44','p45','p46','p47','p48','p49','p50','p51','p52','p53','p54','p55','p56','p57','p58','p59','p60','p61','p62','p63'], 
                'hospital' : ['h1','h1','h1','h2','h2','h2','h2','h3','h3','h3','h3','h3','h4','h4','h4','h4','h4','h4','h5','h5','h5','h5','h5','h5','h5','h6','h6','h6','h6','h6','h6','h6','h6','h7','h7','h7','h7','h7','h7','h7','h7','h7','h8','h8','h8','h8','h8','h8','h8','h8','h8','h8','h9','h9','h9','h9','h9','h9','h9','h9','h9','h9','h9'], 
                'region' : ['r1','r1','r1','r1','r1','r1','r1','r1','r1','r1','r1','r1','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r2','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3','r3'] }

example_dataframe = pd.DataFrame(example_data)

print example_dataframe

這產生如下的簡單輸出:

   hospital patient region
0        h1      p1     r1
1        h1      p2     r1
2        h1      p3     r1
3        h2      p4     r1
4        h2      p5     r1
5        h2      p6     r1
6        h2      p7     r1
7        h3      p8     r1
8        h3      p9     r1
9        h3     p10     r1
10       h3     p11     r1
11       h3     p12     r1
12       h4     p13     r2
13       h4     p14     r2
14       h4     p15     r2
15       h4     p16     r2
16       h4     p17     r2
etc.

現在我想總結使用數據透視表,只計算每家醫院的患者數量:

example_pivot_table = pd.pivot_table(example_dataframe, values='patient', rows=['hospital','region'], aggfunc='count')

print example_pivot_table

這會產生以下輸出:

hospital  region
h1        r1         3
h2        r1         4
h3        r1         5
h4        r2         6
h5        r2         7
h6        r2         8
h7        r3         9
h8        r3        10
h9        r3        11
Name: patient, dtype: int64

據我了解,這實際上是一個多索引系列。

如何使用這些數據找出醫院h7所在的區域? 如果hospitalregion和患者計數數據是DataFrame中的單獨列,那將很容易。 但我認為醫院和地區是指數。 我已經嘗試過很多東西,但卻無法讓它發揮作用。

您可以使用get_level_values來獲取醫院專欄。 您可以傳遞級別數或級別名稱,即0hospital

然后你可以得到你想要的:

In [38]: example_pivot_table[ example_pivot_table.index.get_level_values('hospital') == 'h7' ]
Out[38]: 
hospital  region
h7        r3        9
Name: patient, dtype: int64

更新

要獲得這些地區,你可以做到

example_pivot_table[ example_pivot_table.index.get_level_values('hospital') == 'h7' ]['regions']

首先,這不是一個數據透視表工作,它是一個groupby工作。

樞軸表重塑你的數據時,您沒有設置一個索引(見本文檔文章 ), stackunstack是當你已經設置了index整形和groupby是聚集(這是這是什么)和拆分 - 應用 - 合並操作。

以下是使用groupby計算患者數量的方法:

>>> patient_count = df.groupby(['hospital', 'region']).count()
>>> print patient_count
                 patient
hospital region         
h1       r1            3
h2       r1            4
h3       r1            5
h4       r2            6
h5       r2            7
h6       r2            8
h7       r3            9
h8       r3           10
h9       r3           11

要選擇多索引中的某些行,我通常使用ix ,如下所示:

>>> h7 = patient_count.ix['h7']
>>> print h7
        patient
region         
r3            9

現在你可以使用get_level_values

>>> h7.index.values[0]
'r3'

或者,如果您不想要多索引版本(並且,為了您的目的,您可能不需要),您可以:

>>> patient_count = patient_count.reset_index()

這使您可以找到醫院h7所在的區域如下:

>>> patient_count.region[patient_count.hospital == 'h7']
6    r3
Name: region, dtype: object

如果您只想要r3您可以這樣做:

>>> patient_count.region[patient_count.hospital == 'h7'].values[0]
'r3'

請注意, reset_index不會就地發生,因此很適合鏈接這樣的方法:

>>> patient_count.ix['h7'].reset_index().region[0]
'r3'

這樣就可以了:

levels = example_pivot_table.columns.levels
labels = example_pivot_table.columns.labels
example_pivot_table.columns = levels[1][labels[1]]
example_pivot_table.reset_index(inplace=True)
example_pivot_table

因此,找到數據透視表中的級別和標簽,分配列名稱並重置其中的索引。 最終結果應該是數據透視的結果數據框。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM