我正在尝试遍历列表和数据框,如果列表中的id等于数据框中的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']
首先遍历列表。如果数据框中的id == unique_ids列表中的id,则执行以下操作:
12
是第一行中的最后一项,a1
仍然是与上面相同的ID,因此将第12行设置为第二行中的第二个值。 例如:上述输入的预期输出为
a1,10,12
a1,12,13
a2,14,15
我是如何尝试的:
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
答案 0 :(得分:2)
我认为您想要做的事情可以通过跟踪最后一行的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