maintaining pandas df index with selection & groupby (python)

Viewed 71

I am having an issue with returning the original df index of a row given a groupby condition after subselecting some of the df. It's easier to understand through code. So if we start with a toy dataframe:

headers = ['a','b']
nrows = 8
df = pd.DataFrame(columns = headers)
df['a'] = [0]*(nrows//2) + [1]*(nrows//2)
df['b'] = [2]*(nrows//4) + [4]*(nrows//4) + [2]*(nrows//4) + [4]*(nrows//4)
print(df)

then I select the subset of data I am interested in and checking that the index is retained:

sub_df = df[df['a']==1]    ## selects for only group 1 (indices 4-7)
print(sub_df.index)        ## looks good so far 

sub_df.index returns

Int64Index([4, 5, 6, 7], dtype='int64')

Which seems great! I would like to group data from that subset and extract the original df index and that is where the issue occurs: For example:

g_df = sub_df.groupby('b')
g_df_idx = g_df.indices 
print(g_df_idx)          ## bad!

when I print(g_df_idx) I want it to return:

{2: array([4,5]), 4: array([6,7])}

Due to the way I will be using this code I can't just groupby(['a','b'])

I'm going nuts with this thing. Here are some of the many solutions I have tried:


## 1 
e1_idx = sub_df.groupby('b').indices
# print(e1_idx)                          ## issue persists

## 2
e2 = sub_df.groupby('b', as_index = True) ## also tried as_index = False 
e2_idx = e2.indices 
# print(e2_idx)                          ## issue persists

## 3
e3 = sub_df.reset_index()
e3_idx = e3.groupby('b').indices
# print(e3_idx)                          ## issue persists

I'm sure there must be some simple solution I'm just overlooking. Would be very grateful for any advice.

1 Answers

you can do like this

g_df_idx = g_df.apply(lambda x: x.index).to_dict()
print(g_df_idx)
# {2: Int64Index([4, 5], dtype='int64'), 4: Int64Index([6, 7], dtype='int64')}
Related