2

I'm trying to loop through a list and a data frame where if the id in the list is equal to the id in the data frame, do something to that row in the data frame.

import pandas as pd
data = [['a1','Alex',10],['a1','Bob',12],['a1','Clarke',13],['a2','den',14],['a2','emry',15]]
df = pd.DataFrame(data,columns=['id','Name','Age'])

unique_ids = ['a1','a2']

First loop through the list. If the id in the data frame == the id in the unique_ids list, then do the below:

  • If the unique id in the next row is still the same as the row before, then set the second argument to be the last value from the row above. So, since 12 is the last item in the first row and a1 is still the same id as above, set 12 to be the second value in the second row.

For example: expected output from the input above would be

a1,10,12 
a1,12,13 
a2,14,15

How I tried to do it:

for i in unique_ids:
    for row in df.itertuples(index=True, name='Pandas'):
        while i == getattr(row,"id"):
           print (getattr(row,"id"),getattr(row,"age")
           not sure how to proceed as im getting stuck at the while loop

1 Answer 1

2

I think what you want to do can be done by keeping track of the last row's id.

import pandas as pd
data = [['a1','Alex',10],['a1','Bob',12],['a1','Clarke',13],['a2','den',14],['a2','emry',15]]
df = pd.DataFrame(data,columns=['id','Name','Age'])

unique_ids = ['a1','a2']
last_id = df.iloc[0]['id']  # initilize to the first row's id
for idx, row in df[1:].iterrows():  
    if row['id'] in unique_ids and row['id'] == last_id:
        # You can retrieve last row by df.iloc[idx-1]
        print(row['id'], ",", df.iloc[idx-1]['Age'], ",", row['Age']) 
    last_id = row['id'] # update last_id

Output:
a1 , 10 , 12
a1 , 12 , 13
a2 , 14 , 15
Sign up to request clarification or add additional context in comments.

Comments

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.