Pandas percentage of total with groupby

前端 未结 14 2490
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 06:41

This is obviously simple, but as a numpy newbe I\'m getting stuck.

I have a CSV file that contains 3 columns, the State, the Office ID, and the Sales for that office

14条回答
  •  无人共我
    2020-11-22 07:05

    I think this would do the trick in 1 line:

    df.groupby(['state', 'office_id']).sum().transform(lambda x: x/np.sum(x)*100)
    

提交回复
热议问题