0

I would like to extract lists of indexes based on the value of column ID.

data={'ID':[1,1,2,3,6,4,2,6], 'Number': [10,20,5,6,100,90,40,5]}

df=pd.DataFrame(data)

I know how to do that manually, one value/list at a time:

 idx_list=df.index[df.ID == 1].tolist()

but in my code, I usually don't know how many different values of ID I have, so the above approach would not be enough. Ideally I would like to have multiple lists as output. for each value of ID, a list of indexes.

3
  • Do you atleast would know the different values of id contained in some list on which you can filter Commented Jul 17, 2022 at 7:23
  • @HimanshuPoddar I just know that most likely it would be an integer number between [1,5]. Commented Jul 17, 2022 at 7:25
  • I think you need isin Commented Jul 17, 2022 at 7:26

2 Answers 2

1

You can use a for loop

idx_list = []
for ID in data["ID"]:
    idx_list.append(df.index[df.ID == ID].tolist())

This will give you the indices for each ID. Note that there will be duplicates. To avoid this, only add to idx_list if the value is already not present:

idx_list = []
for ID in data["ID"]:
    if df.index[df.ID == ID].tolist() not in idx_list: idx_list.append(df.index[df.ID == ID].tolist())
Sign up to request clarification or add additional context in comments.

Comments

1

You can store the list of indexes for each value you want to filter for in aseparate container

i_list=[]
for x in df.ID:
    i_list.append(df.index[df['ID'] == x].tolist())

i_list contains the list of indexes as a 2D list

1 Comment

Thank you for your answer @Himanshu Poddar, ideally I would like to have multiple lists as output. for each value of ID, a list of indexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.