I generated the following pivot table via taking maximum of values in Z
column:
val
X x1 x2
Y y1 y2 y1 y2
ID
a 9 1 5 11
b 8 10 7 6
After taking max on Z
values, I need to report mean(y1,y2)
. The desired table is:
val
X x1 x2
Y mean(y1,y2) mean(y1,y2)
ID
a 5 8
b 9 6.5
How can I achieve this using pandas?
My 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