0

I calculate standard deviations in a for loop as such:

times = os.listdir(r'filepathway')
    for file in times:
        if file.endswith('.txt'):
           ...
           ...
           z = df.iloc[1004:1255,:]
           y = np.std(z)
           print(y)

and I get a list of values. But when I print(y) in the next cell (outside of the loop), I don't get all the values. How can I basically extract the data from the loop into the next cell?

1
  • you need to store y value from each iteration of the loop in some sort of data structure, e.g. list. Then, after the loop, print this list. Also take a look at glob.glob() - you can specify pattern (path/*.txt) and it will return only txt files - no need to check the extension Commented Dec 20, 2018 at 18:44

1 Answer 1

1

The reason of why you can even print(y) is because the variable remains with the last value it had. You need to save all those values in a list:

result = []

times = os.listdir(r'filepathway')
for file in times:
    if file.endswith('.txt'):
       ...
       ...
       z = df.iloc[1004:1255,:]
       y = np.std(z)

       result.append(y)

print(result)
Sign up to request clarification or add additional context in comments.

2 Comments

@D.Kim, please, if this answer worked for you, mark it as accepted (the green checkmark ✓)
Yes that worked! I was waiting for stack overflow to let me accept your answer :)

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.