pandas數據透視表pivot_table

簡介

類似excel的數據透視表功能,pandas提供了pivot_table和pivot實現數據透視表的功能,具體過程是:分組→聚合→展示。

1、pivot_table()數據透視+聚合

語法: pandas.pivot_table(data, index=None, columns=None, values=None, aggfunc=‘mean’, fill_value=None, dropna=True, margins=False, margins_name=‘All’, observed=False)
Parameters:

  • data: DataFrame
  • index: column, Grouper, array, or list of the previous。指定放在行索引的列,類似excel數據透視表的“行”。
  • columns: column, Grouper, array, or list of the previous。指定放在列索引的列,類似excel數據透視表的”列”。
  • values: column to aggregate, optional。要計算的列,求和、計數、均值等,類似excel的”值”。
  • aggfunc: function, list of functions, dict, default numpy.mean。指定要對values進行的運算,如果values是多列,dict可分別爲其指定計算方式。
  • fill_value: scalar, default None. Value to replace missing values with.
  • dropna: boolean, default True。剔除透視後全是NaN的列。
  • margins: boolean, default False。是否添加彙總列,excel數據透視表默認添加。
  • margins_name: string, default ‘All’. Name of the row / column that will contain the totals when margins is True.
df = pd.DataFrame({'學校':['一中', '一中', '一中', '一中', '一中', '一中', '一中', '一中', '一中', '一中', '二中', '二中', '二中', '二中', '二中', '二中'],
                  '班級':['1班', '1班', '1班', '1班', '1班', '1班', '2班', '2班', '2班', '3班', '1班', '1班', '1班', '2班', '2班', '2班'],
                  '組別':['1組', '1組', '2組', '2組', '2組', '2組', '1組', '2組', '3組', '1組', '1組', '1組', '2組', '1組', '1組', '2組'],
                  '姓名':['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'],
                  '身高':[165, 165, 160, 160, 160, 160, 158, 150, 157, 170, 175, 175, 180, 176, 176, 177],
                  '體重':[45, 45, 55, 55, 55, 55, 54, 46, 60, 65, 70, 70, 75, 73, 73, 76],
                  '數學成績':[65, 65, 75, 75, 75, 75, 74, 66, 80, 85, 90, 90, 95, 93, 93, 96]})

df
Out[57]: 
    學校  班級  組別 姓名   身高  體重  數學成績
0   一中  11組  a  165  45    65
1   一中  11組  b  165  45    65
2   一中  12組  c  160  55    75
3   一中  12組  d  160  55    75
4   一中  12組  e  160  55    75
5   一中  12組  f  160  55    75
6   一中  21組  g  158  54    74
7   一中  22組  h  150  46    66
8   一中  23組  i  157  60    80
9   一中  31組  j  170  65    85
10  二中  11組  k  175  70    90
11  二中  11組  l  175  70    90
12  二中  12組  m  180  75    95
13  二中  21組  n  176  73    93
14  二中  21組  o  176  73    93
15  二中  22組  p  177  76    96

pd.pivot_table(df, index=['學校', '班級'], columns=['組別'], values=['身高', '體重'], aggfunc={'身高':'sum', '體重':['max', 'min']}, fill_value='missing')
#結果見下:

結果展示

2、pivot()數據透視,不支持聚合

語法: pandas.pivot(data, index=None, columns=None, values=None)
參數說明: 與pivot_table類似

  • data: DataFrame
  • index: string or object, optional
  • columns: string or object
  • values: string, object or a list of the previous, optional
 df = pd.DataFrame({"foo": ['one', 'one', 'two', 'two'],
                    "bar": ['A', 'A', 'B', 'C'],
                    "baz": [1, 2, 3, 4]})
df
Out[69]: 
   foo bar  baz
0  one   A    1
1  one   A    2
2  two   B    3
3  two   C    4

df.pivot(index='foo', columns='bar', values='baz')
Traceback (most recent call last):
...
ValueError: Index contains duplicate entries, cannot reshape
發佈了8 篇原創文章 · 獲贊 0 · 訪問量 249
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章