pivot table (透视表)

    pands.pivot_table(data, values=None, index=None, columns=None, aggfunc=’mean’, fill_value=None, margins=False, dropna=True, margins_name=’All’)

    1. #参数名称 说明
    2. #data 接收DataFrame。表示创建表的数据。无默认。
    3. #values 接收字符串。用于指定想要聚合的数据字段名,默认使用全部数据。默认为None。
    4. #index 接收string或list。表示行分组键。默认为None。
    5. #columns 接收string或list。表示列分组键。默认为None。
    6. #aggfunc 接收functions。表示聚合函数。默认为mean。
    7. #margins 接收boolearn。表示汇总(Total)功能的开关,设为True后结果集中会出现名为“ALL”的 行和列。默认为True。
    8. #dropna 接收boolearn。表示是否删掉全为NaN的列。默认为False。
    1. import pandas as pd
    2. import numpy as np
    3. df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
    4. "bar", "bar", "bar", "bar"],
    5. "B": ["one", "one", "one", "two", "two",
    6. "one", "one", "two", "two"],
    7. "C": ["small", "large", "large", "small",
    8. "small", "large", "small", "small",
    9. "large"],
    10. "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
    11. "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
    12. df
    A B C D E
    0 foo one small 1 2
    1 foo one large 2 4
    2 foo one large 2 5
    3 foo two small 3 5
    4 foo two small 3 6
    5 bar one large 4 6
    6 bar one small 5 8
    7 bar two small 6 9
    8 bar two large 7 9
    1. table = pd.pivot_table(df, values='D', index=['A', 'B'],
    2. columns=['C'], aggfunc=np.sum)
    3. table
    C large small
    A B
    bar one 4.0 5.0
    two 7.0 6.0
    foo one 4.0 1.0
    two NaN 6.0
    1. table = pd.pivot_table(df, values='D', index=['A', 'B'],
    2. columns=['C'], aggfunc='sum', fill_value=0) #填充值
    3. table
    C large small
    A B
    bar one 4 5
    two 7 6
    foo one 4 1
    two 0 6