简体   繁体   中英

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM