Home > Net >  Creating lists of indexes based on a data frames column's values
Creating lists of indexes based on a data frames column's values

Time:07-17

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.

CodePudding user response:

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

CodePudding user response:

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())
  • Related