例子:
import pandas as pd
data = {'id':[101,101,101,101,102,102,102,102],
'day':[1,2,1,2,1,2,1,2],
'year':[2011,2011,2012,2012,2011,2011,2012,2012],
'avg':[0.500,0.400,0.300,0.200,0.555,0.455,0.355,0.255],
'sum':[1, 2, 2, 3, 6, 6, 8, 9],
'div':[2, 1, 3, 2, 6, 1, 6, 3]}
df = pd.DataFrame(data)
df
id day year avg sum div
0 101 1 2011 0.500 1 2
1 101 2 2011 0.400 2 1
2 101 1 2012 0.300 2 3
3 101 2 2012 0.200 3 2
4 102 1 2011 0.555 6 6
5 102 2 2011 0.455 6 1
6 102 1 2012 0.355 8 6
7 102 2 2012 0.255 9 3
所需的输出:
id sum div 2011_avg 2012_avg 2011_sum 2012_sum 2011_div 2012_div
0 101 8 8 0.450 0.250 3 5 2 1.5
1 102 29 16 0.505 0.305 12 17 6 2.0
我按年份为每一列制作了几个 pivot_tables 并多次加入..
谁能给我一些知识,让我更容易或有效地获得所需的输出?
梵蒂冈之花
大话西游666
相关分类