How to summarize on different groupby combinations?

后端 未结 5 1566
后悔当初
后悔当初 2020-12-04 02:59

I am compiling a table of top-3 crops by county. Some counties have the same crop varieties in the same order. Other counties have the same crop varieties in a different ord

5条回答
  •  长情又很酷
    2020-12-04 03:39

    np.bincount

    i, u = pd.factorize([*map(frozenset, zip(df1.Crop1, df1.Crop2, df1.Crop3))])
    s = np.bincount(i, df1.Total_pop)
    
    pd.Series(s, u)
    
    (melons, grain, apples)      8000.0
    (carrots, raddish, pears)    9200.0
    dtype: float64
    

    Or, if you want separate columns

    pd.Series(dict(zip(map(tuple, u), s)))
    
    melons   grain    apples    8000.0
    carrots  raddish  pears     9200.0
    dtype: float64
    

    And fully pretty

    pd.Series(dict(zip(map(tuple, u), s))) \
      .rename_axis(['Crop1', 'Crop2', 'Crop3']).reset_index(name='Total_pop')
    
         Crop1    Crop2   Crop3  Total_pop
    0   melons    grain  apples     8000.0
    1  carrots  raddish   pears     9200.0
    

提交回复
热议问题