Pandas-重塑数据帧和Groupby

时间:2017-12-25 12:16:06

标签: pandas pandas-groupby

我的数据框看起来像这样,

let database = [{
        name: 'James Bond',
        code: '007'
    },
    {
        name: 'El',
        code: '11'
    }
]

let subject = {
    name: 'James Bond',
    code: '007'
}

console.log(database.findIndex(x => x.name=="James Bond"))

我想重塑数据帧(不同时期为列,prod为行) 和groupby期间,loc 预期的O / P

    loc         prod   period   qty        
0   Customer10  FG1    2483  200.000000    
1   Customer10  FG1    2484  220.000000    
2   Customer10  FG1    2485  240.000000    
3   Customer10  FG1    2486  260.000000      
4   Customer11  FG1    2483  300.000000    
5   Customer11  FG1    2484  320.000000    
6   Customer11  FG1    2485  340.000000    
7   Customer11  FG1    2486  360.000000    
8   Customer12  FG1    2483  400.000000    
9   Customer12  FG1    2484  420.000000    
10  Customer12  FG1    2485  440.000000    
11  Customer12  FG1    2486  460.000000    
12  Customer13  FG1    2483  500.000000    
13  Customer13  FG1    2484  520.000000    
14  Customer13  FG1    2485  540.000000    
15  Customer13  FG1    2486  560.000000    
16   Customer9  FG1    2483  100.000000    
17   Customer9  FG1    2484  120.000000    
18   Customer9  FG1    2485  140.000000    
19   Customer9  FG1    2486  160.000000    

1 个答案:

答案 0 :(得分:2)

您可以使用pivot_table

In [37]: df.pivot_table(index='prod', columns='period', values='qty', aggfunc='sum')
Out[37]:
period    2483    2484    2485    2486
prod
FG1     1500.0  1600.0  1700.0  1800.0

In [39]: df.groupby(['prod','period'])['qty'].sum().unstack()
Out[39]:
period    2483    2484    2485    2486
prod
FG1     1500.0  1600.0  1700.0  1800.0

<强>更新

  

如何获得max(qty)的时间段?

In [69]: pvt = df.pivot_table(index='prod', columns='period', values='qty', aggfunc='sum')

In [70]: pvt
Out[70]:
period    2483    2484    2485    2486
prod
FG1     1500.0  1600.0  1700.0  1800.0

In [71]: pvt.idxmax(axis=1)
Out[71]:
prod
FG1    2486
dtype: int64