jxn
jxn

Reputation: 8035

Pandas loop through dataframe and list with while loop

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:

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

Upvotes: 2

Views: 3303

Answers (1)

Tai
Tai

Reputation: 7994

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

Upvotes: 2

Related Questions