【发布时间】:2018-11-22 06:13:07
【问题描述】:
import pandas as pd
df = pd.DataFrame({
'customer': [1,2,1,3,1,2,3],
"group_code": ['111', '111', '222', '111', '111', '111', '333'],
"ind_code": ['A', 'B', 'AA', 'A', 'AAA', 'C', 'BBB'],
"amount": [100, 200, 140, 400, 225, 125, 600],
"card": ['XXX', 'YYY', 'YYY', 'XXX', 'XXX', 'YYY', 'XXX']})
有了上面的数据框,我想要的输出如下:
对于每个card number,我想要以下记录:
Card number, % of Amount spent of Group code 1, % of Amount spent on Group code 2, ....so on不同的组代码
% of Amount spent on any group = (Total amount spend on the card / Amount spend on that group ) * 100
另外,在更大的图片中,我想知道每张卡消费金额的前 5 个组?
基本上是2个查询,如果有人可以帮助我,那就太好了。
注意:给出的代码只是为了了解我的数据框的样子。
【问题讨论】:
标签: python pivot-table pandas-groupby