Pandas percentage of total with groupby

前端 未结 14 2472
没有蜡笔的小新
没有蜡笔的小新 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 06:57

    df = pd.DataFrame({'state': ['CA', 'WA', 'CO', 'AZ'] * 3,
                   'office_id': list(range(1, 7)) * 2,
                   'sales': [np.random.randint(100000, 999999)
                             for _ in range(12)]})
    
    grouped = df.groupby(['state', 'office_id'])
    100*grouped.sum()/df[["state","sales"]].groupby('state').sum()
    

    Returns:

    sales
    state   office_id   
    AZ  2   54.587910
        4   33.009225
        6   12.402865
    CA  1   32.046582
        3   44.937684
        5   23.015735
    CO  1   21.099989
        3   31.848658
        5   47.051353
    WA  2   43.882790
        4   10.265275
        6   45.851935
    

提交回复
热议问题