pandas - Aggregate dataframe indices in python -


i want aggregate indices of dataframe groupby function.

     word  count 0        3 1      5 2        3 3       2 4      1 

what want pd.series consists of list(descending order) of indices,

word       [2, 0]         [3]     [4, 1] 

i've tried built-in functions groupby, however, couldn't find way aggregate indices. provide hint or solution problem?

i think can first change order of index [::-1], groupby , apply index list. last sort_index:

print (df[::-1].groupby('word', sort=false).apply(lambda x: x.index.tolist()).sort_index()) word      [2, 0]        [3]    [4, 1] dtype: object 

another similar solution:

print (df.sort_index(ascending=false)          .groupby('word', sort=false)          .apply(lambda x: x.index.tolist())          .sort_index()) word      [2, 0]        [3]    [4, 1] dtype: object 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -