0

Forgive what is likely an elementary question, but I'm struggling to parse a .csv file into a list. Within the "For" loop, I get the full list of items I want, but outside of it, it only returns the last iterated row.

'''

for i in subset.index:
    alta = pd.DataFrame()
    transactions = []
    transactions.append(subset['Date'][i])
    transactions.append(subset['Credit Amount'][i])
    transactions.append(subset['Debit Amount'][i])
    transactions.append(subset['Description'][i])
    print(transactions)

'''

This returns the below:

'''

['9/28/2018', nan, 46494533.33, 'SWEEP TO TREAS REPO I']
['9/28/2018', 46521123.39, nan, 'Invest-SWEEP TREASURY REPO - ICBC']
['9/28/2018', nan, 26590.06, 'SENDER BNK:=SIL VLY BK SCLA; SENDER ID:=121140399; SENDER REF:=20172799961800; ORG ID:=3300000000; ORG:=HL ADVISORS; ORG ADDRESS:=PO BOX 1 NEW YORK']
['9/28/2018', 10150015.1, nan, 'SENDER BNK:=SIL VLY BK SCLA; SENDER ID:=121140399; SENDER REF:=201809280156999; ORG ID:=3300000000; ORG:=WB ADVISORS; ORG ADDRESS:=PO BOX 1 NEW YORK']
['9/28/2018', nan, 35.0, nan]
['9/28/2018', nan, 670000.0, nan]

'''

When I run 'print(transactions" out side the "For" loop, I only get the below:

['9/28/2018', nan, 670000.0, nan]

Any suggestions are much appreciated!

2
  • 1
    On the third line, you're overwriting the "transactions" variable with a new list during every iteration of the loop. Create the list before the loop. Commented Sep 14, 2019 at 16:12
  • 1
    You aren't returning anything; you are writing a string representation of the current transaction to standard output in each iteration.
    – chepner
    Commented Sep 14, 2019 at 16:18

2 Answers 2

2

Each transactions list corresponds to one iteration (because you initialize it inside the loop). At the end, you'll have the results of the last iteration. To keep track of all of them, you need to append each transactions list, into a "global transactions list", defined outside of the loop.

transactions_list = []
for i in subset.index:
    transactions = []
    # ...
    transactions_list.append(transactions)

print(transactions_list)
1
  • Thank you, CristiFati. Your guidance is much appreciated!
    – terrapin
    Commented Sep 18, 2019 at 3:48
0

Due to the fact that your transactions=[] list is inside the for loop, it is being overwritten each time you loop through it.

transactions=[]
for item in items:
    list_item = "something"
    transactions.append(list_item)

These kind of learning questions are better suited for pythondev.slack.com to help you get on your feet.

2
  • This is wrong. why use _? and you should not use iter for naming variables.
    – Netwave
    Commented Sep 14, 2019 at 16:43
  • edited. the point in _ was to keep the focus on the subject at hand
    – Kermit
    Commented Sep 14, 2019 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.