1 回答
TA贡献1830条经验 获得超3个赞
您可以传递一个带有标量函数名称和列表的字典,但有必要展平 MultiIndex 和rename列:
data_open_close_inv = (data.sort_values(['Date','Tank','Time'])
.groupby(['Date','Tank'])
.agg({'Sales':'sum','Delivery':'sum', 'Quantity':['first','last']})
.reset_index())
data_open_close_inv.columns = data_open_close_inv.columns.map(''.join)
d = {'Salessum':'Sales',
'Delivery_sum':'Delivery',
'Quantityfirst':'Open',
'Quantitylast':'Close',
}
data_open_close_inv = data_open_close_inv.rename(columns=d)
print (data_open_close_inv)
Date Tank Sales Deliverysum Open Close
0 2018-01-01 1 600 0 2900 2800
1 2018-01-01 2 400 0 2800 2704
2 2018-01-02 1 600 50 2900 2800
3 2018-01-02 2 400 50 2800 2704
添加回答
举报