0

I am trying get each data point from a data point with 13 columns and 13xx rows. And I figured I could make a nested loop to get each data point, however the code below is not working:

i = 0
for row in data.itertuples():
    while i < len(da) - 1:
        price = row[i:i+1]
        price, = price
        print(price)
        i += 1

These are the only values I get (which is from one row only). How do I get all of the rows?

2011-12-12 00:00:00 64.58 64.92 63.935 64.31 8793500.0 0.0 1.0 53.7727555366 54.0558576872 53.235694104

1
  • You never reset i after looping once, which I'd think would be necessary. Surprised this doesn't throw an OOB exception. And what's da? Do you mean row? Commented Oct 18, 2017 at 15:08

3 Answers 3

2

Move the counter into the for block:

for row in data.itertuples():
    i = 0
    while i < len(da) - 1:
        price = row[i:i+1]
        price, = price
        print(price)
        i += 1
Sign up to request clarification or add additional context in comments.

1 Comment

Yes should have seen that myself. Thank you.
0

Why not an actual nested loop?

for row in data:
    for column in row:
        print(column)

1 Comment

He is getting two values from each row and then he is extracting just one. It may have been a simplification for SO but he does need multiple values in the inner loop. Just guessing.
0

I am not a Python expert but if I were writing this in C# I would nest for's instead of using the while. In my experience you should use while only when it's appropriate and in this case we have a much easier way of looping this code. You've already determined the rows with your first loops so the next steps to display columns would be to search those rows for the columns. If you want to stick with the "i=0" then foreach column is the way to go. However, foreach is more taxing on your program than just using for and while isn't needed(for is twice as fast as foreach). You want to avoid the possibility of going into the infinite loop hell. Again not a python expert but using just a for statement to find the columns, as Daniel above suggested, seems like it would work great here.

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.