簡體   English   中英

帶有平均值的熊貓數據透視表

[英]Pandas pivot table with mean

我有一個熊貓數據框df,看起來像這樣;

index  New  Old  MAP      Limit  count
1       93   35   54       > 18      1
2      163   93  116       > 18      1
3      134   78   96       > 18      1
4      117   81   93       > 18      1
5      194  108  136       > 18      1
6      125   57   79      <= 18      1
7       66   39   48       > 18      1
8      120   83   95       > 18      1
9      150   98  115       > 18      1
10     149   99  115       > 18      1
11     148   85  106       > 18      1
12      92   55   67      <= 18      1
13      64   24   37       > 18      1
14      84   53   63       > 18      1
15      99   70   79       > 18      1

我需要創建一個像這樣的數據透視表

Limit   <=18   >18
New      xx1   xx2
Old      xx3   xx4
MAP      xx5   xx6

其中值xx1,xx2,xx3,xx4,xx5和xx6是相應限制的新,舊和映射的平均值。 我該如何實現? 我嘗試了以下方法,但沒有成功。

table = df.pivot_table('count', index=['New', 'Old', 'MAP'], columns=['Limit'], aggfunc='mean')

df.groupby('Limit')['New', 'Old', 'MAP'].mean().T

Limit  <= 18        > 18
New    108.5  121.615385
Old     56.0   72.769231
MAP     73.0   88.692308

暫無
暫無

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

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