我通过获取Z
列中的最大值来生成以下数据透视表:
val
X x1 x2
Y y1 y2 y1 y2
ID
a 9 1 5 11
b 8 10 7 6
在获取Z
值的最大值后,我需要报告mean(y1,y2)
。所需的表格是:
val
X x1 x2
Y mean(y1,y2) mean(y1,y2)
ID
a 5 8
b 9 6.5
如何使用pandas实现这一目标?
我的MWE:
#!/usr/bin/python
from pandas import DataFrame
import pandas as pd
import numpy as np
data=pd.read_table('data.txt')
pv=data.pivot_table(index=['ID'], columns=['X','Y'], values=['val'], aggfunc=np.max )
print pv
data.txt
:
ID X Y Z val
a x1 y2 z1 1
b x1 y1 z2 2
a x2 y2 z2 3
a x1 y1 z4 4
a x2 y1 z1 5
b x2 y2 z3 6
b x2 y1 z2 7
b x1 y1 z3 8
a x1 y1 z3 9
b x1 y2 z3 10
a x2 y2 z2 11