How do I get the index of each item in a groupby object in Pandas?

GiannisIordanou

I use groupby on a dataframe based on the columns I want and then I have to take the index of each item in its group. By index I mean, if there are 10 items in a group, the index goes from 0 to 9, not the dataframe index.

My code for doing this is below:

import pandas as pd

df = pd.DataFrame({'A': np.random.randint(0, 11, 10 ** 3), 'B': np.random.randint(0, 11, 10 ** 3), 
                   'C': np.random.randint(0, 11, 10 ** 3), 'D': np.random.randint(0, 2, 10 ** 3)})

grouped_by = df.groupby(["A", "B", "C"])
groups = dict(list(grouped_by))
index_dict = {k: v.index.tolist() for k,v in groups.items()}
df["POS"] = df.apply(lambda x: index_dict[(x["A"], x["B"], x["C"])].index(x.name), axis=1)

The dataframe here is just an example.

Is there a way to use the grouped_by to achieve this ?

chrisb

Here's a solution using cumcount() on a dummy variable to generate a item index for each group. It should be significantly faster too.

In [122]: df['dummy'] = 0
     ...: df["POS"] = df.groupby(['A','B','C'])['dummy'].cumcount()
     ...: df = df.drop('dummy', axis=1)

As @unutbu noted, even cleaner just to use:

df["POS"] = df.groupby(['A','B','C']).cumcount()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the index and occurance of each item using itertools.groupby()

From Dev

How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

From Dev

How do i get Object index in an array

From Dev

How do I get the index of an object in this array?

From Dev

How do i get the ComboBox items text of each item?

From Dev

How do I get a list item by index in elm?

From Dev

How do I get a key value of each object in a javascript object?

From Dev

Pandas : Python how to do index Groupby with replicates

From Dev

How do I test if an object is a pandas datetime index?

From Dev

How do I get the key for each object in a dynamic JSON array?

From Dev

How could I count item based on each index by using pandas in python?

From Dev

How to get number of groups in a groupby object in pandas?

From Dev

How do I get all the rows before a specific index in Pandas?

From Dev

How do i get the index in an for each loop and how can i do more than 1 operation?

From Dev

How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

From Dev

How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

From Dev

Get the index of each item in a loop

From Dev

How do I get the link text using Beautiful Soup for each item in my scraper?

From Dev

How to get the index of each item that is null in a List<T> via Linq

From Dev

How to get a list of index values after groupby().mean() in pandas?

From Dev

How do I get the id of a list item?

From Dev

How can I get index of rows in pandas?

From Dev

How do I get first item from CakePHP 3 results object

From Dev

how to get the index of ith item in pandas.Series

From Dev

pandas groupby object index turns into FUBAR

From Dev

How do I get the hash of each link?

From Dev

How do I get the sum of years from date column in reference to each ticker?(Pandas)

From Dev

How do I return one item at a time with each button click?

From Dev

How do I generate a list for each list item

Related Related

  1. 1

    How to get the index and occurance of each item using itertools.groupby()

  2. 2

    How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

  3. 3

    How do i get Object index in an array

  4. 4

    How do I get the index of an object in this array?

  5. 5

    How do i get the ComboBox items text of each item?

  6. 6

    How do I get a list item by index in elm?

  7. 7

    How do I get a key value of each object in a javascript object?

  8. 8

    Pandas : Python how to do index Groupby with replicates

  9. 9

    How do I test if an object is a pandas datetime index?

  10. 10

    How do I get the key for each object in a dynamic JSON array?

  11. 11

    How could I count item based on each index by using pandas in python?

  12. 12

    How to get number of groups in a groupby object in pandas?

  13. 13

    How do I get all the rows before a specific index in Pandas?

  14. 14

    How do i get the index in an for each loop and how can i do more than 1 operation?

  15. 15

    How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

  16. 16

    How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

  17. 17

    Get the index of each item in a loop

  18. 18

    How do I get the link text using Beautiful Soup for each item in my scraper?

  19. 19

    How to get the index of each item that is null in a List<T> via Linq

  20. 20

    How to get a list of index values after groupby().mean() in pandas?

  21. 21

    How do I get the id of a list item?

  22. 22

    How can I get index of rows in pandas?

  23. 23

    How do I get first item from CakePHP 3 results object

  24. 24

    how to get the index of ith item in pandas.Series

  25. 25

    pandas groupby object index turns into FUBAR

  26. 26

    How do I get the hash of each link?

  27. 27

    How do I get the sum of years from date column in reference to each ticker?(Pandas)

  28. 28

    How do I return one item at a time with each button click?

  29. 29

    How do I generate a list for each list item

HotTag

Archive