Basically I have a list(csv file) that I read using pandas and I need to mark/flag-it if the current file is the first or the last of that list. The problem is that I am not quite sure how to reference the previous iteration from the list. I tried shift() but can you recomment another way. thank you. enter image description here

for index, row in df.iterrows():

    results = row['Transaction_Type']
    df = pd.DataFrame(result)
    result = pd.result(result).tail(1)

    if result:
        print(result)
        trans_end.append(1)
    else:
        empty_sap = True
        trans_end.append(0)
    print('', end = '', flush = True)
df['End_Transaction'] = trans_end

link to the csv

1 Answers

0
Erick Peirson On

Depending on how your DataFrame is indexed there might be a simpler way, but: have you considered using Python's built-in enumerate, and comparing the count to the shape of the DataFrame?

for i, (index, row) in df.iterrows():
    if i == 0:
        print("first row!")
    elif i == df.shape[0] - 1:
        print("last row!")