Remove original index when iterating through groups with groupby

Viewed 26

I have data in a multiindex dataframe and iterate through it by using groupby to plot parts of the dataframe. When doing so, the original index levels are preserved but I would like to remove them. For example, using this MWE:

input = {('a',0,'alpha'): [1,2],('b',1,'alpha'): [3,4],('c',2,'beta'): [5,6]}
df = pd.DataFrame.from_dict(input).T
df

                0   1
a   0   alpha   1   2
b   1   alpha   3   4
c   2   beta    5   6

for group, sub_df in df.groupby(level=[0,1]):
    print(group)
    print(sub_df)

('a', 0)
           0  1
a 0 alpha  1  2
('b', 1)
           0  1
b 1 alpha  3  4
('c', 2)
          0  1
c 2 beta  5  6

We see that, for example in the first group, sub_df contains the first two levels of indices ('a',0). I would like to remove these. I usually use droplevel() on each sub_df but would like to know if there is a more direct way to do it (i.e. that grouped dataframes when iterating with groupby() don't contain the levels used in groupby)

1 Answers

IIUC use if need default index, here 0 use:

for group, sub_df in df.groupby(level=[0,1]):
    print(group)
    print(sub_df.reset_index(drop=True))
('a', 0)
   0  1
0  1  2
('b', 1)
   0  1
0  3  4
('c', 2)
   0  1
0  5  6

If need remeove only some levels, e.g. first and second use:

for group, sub_df in df.groupby(level=[0,1]):
    print(group)
    print(sub_df.droplevel([0,1]))
('a', 0)
       0  1
alpha  1  2
('b', 1)
       0  1
alpha  3  4
('c', 2)
      0  1
beta  5  6
Related