Get all keys from GroupBy object in Pandas

后端 未结 3 631
小鲜肉
小鲜肉 2020-12-28 12:55

I\'m looking for a way to get a list of all the keys in a GroupBy object, but I can\'t seem to find one via the docs nor through Google.

There is definitely a way t

3条回答
  •  生来不讨喜
    2020-12-28 13:25

    A problem with EdChum's answer is that getting keys by launching gp.groups.keys() first constructs the full group dictionary. On large dataframes, this is a very slow operation, which effectively doubles the memory consumption. Iterating is waaay faster:

    df = pd.DataFrame({'group':list('bgaaabxeb'), 'val':np.arange(9)})
    gp = df.groupby('group')
    keys = [key for key, _ in gp]
    

    Executing this list comprehension took me 16 s on my groupby object, while I had to interrupt gp.groups.keys() after 3 minutes.

提交回复
热议问题