python库--pandas--部分实例

 

>>> pd.pivot(
    index=np.array(['one', 'one', 'one', 'two', 'two', 'two']), 
    columns=np.array(['A', 'B', 'C', 'A', 'B', 'C']), 
    values=np.array([1,2,3,4,5,6]))

out:
     A  B  C
one  1  2  3
two  4  5  6
>>> df
out:
     A    B      C  D
0  foo  one  small  1
1  foo  one  large  2
2  foo  one  large  2
3  foo  two  small  3
4  foo  two  small  3
5  bar  one  large  4
6  bar  one  small  5
7  bar  two  small  6
8  bar  two  large  7

>>> pd.pivot_table(df, values='D', index=['A', 'B'],columns=['C'], aggfunc='sum', margins=True)
out:
C        large  small  All
A   B
bar one    4.0    5.0    9
    two    7.0    6.0   13
foo one    4.0    1.0    5
    two    NaN    6.0    6
All       15.0   18.0   33

 

posted @ 2018-04-06 17:11  _殇  阅读(769)  评论(0编辑  收藏  举报