How to get the first group in a groupby of multiple columns?

北城余情 提交于 2020-07-06 12:56:11

问题


I've been trying to figure out how I can return just the first group, after I apply groupby.

My code looks like this:

gb = df.groupby(['col1', 'col2', 'col3', 'col4'])['col5'].sum()

What I want is for that first first group to output. I've been trying the get_group method but it keeps failing (maybe because I am grouping by multiple columns?)

Here is an example of my output:

col1  col2  col3   col4  'sum'
 1     34   green   10    0.0
            yellow  30    1.5 
            orange  20    1.1 
 2     89   green   10    3.0 
            yellow   5    0.0 
            orange  10    1.0

What I want to be returned is just this:

col1  col2  col3   col4  'sum'
 1     34   green   10    0.0
            yellow  30    1.5 
            orange  20    1.1 

(Note the 'sum' column I just added here to make it clear what that last column was, but pandas does not actually name that column)


回答1:


You can using get_group with groups

g=df.groupby(['col1','col2'])

g.get_group((list(g.groups)[0])).groupby(['col3','col4'])['col5'].sum()



回答2:


gb = df.groupby(['col1', 'col2', 'col3', 'col4'])['col5'].sum()

gb.loc[[gb.index.levels[0][0]]])



回答3:


I believe you need:

idx = df.index.get_level_values(0)
df = df[idx == idx[0]] 

Or DataFrame.xs:

df = df.xs(df.index.levels[0][0])

print (df)
                       'sum'
col1 col2 col3   col4       
1    34   green  10      0.0
          yellow 30      1.5
          orange 20      1.1


来源:https://stackoverflow.com/questions/49799731/how-to-get-the-first-group-in-a-groupby-of-multiple-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!