0

I have a Dataframe of 50 columns and 2000+ rows of data. I basically want to go through each column row by row and check if the value in the column becomes greater than 10 BEFORE it becomes less than -10. If so, iterate a counter and goto the next column.

for row in data2.transpose().iterrows():
    if row > 10:
        countTP = countTP + 1
        break
    if row < -10:
        countSL = countSL + 1
        break

print countTP, countSL

Out: 1 0

At least half the columns should have iterated the counter (I believe the answer should be 35 15) Appreciate any help!

1 Answer 1

1

It seems that you are confused what you want to do.

  1. .iterrows() gives you a tuple. So your row will actually be a tuple of the form (column_name, Series). A tuple is always greater than an integer. For example, read this one. So row > 10 is always True and row<-10 always False.
  2. Once you enter the loop, row > 10 evaluates True and it breaks and you are done right away. There's no more looping and no more checking (Maybe you were thinking continue but the row-wise comparison doesn't work as expected.) That's why you always get countTP=1 and countSL=0.

What you really want is something like the following nested loops. (One may argue that this is slow and vectorization is preferred. But this is for clearity and for an 2000+ by 50 dataframe, this isn't too slow.)

countTP = countSL = 0

for col_name in data2:
    for row in data2[col_name]:
        if row > 10:
            countTP = countTP + 1
            break #Here we want to 'break' out of the inner loop but continue the outer loop
        if row < -10:
            countSL = countSL + 1
            break

print countTP, countSL
Sign up to request clarification or add additional context in comments.

1 Comment

This is awesome thanks! Out of curiosity, what would be the more 'efficient' way of doing this?

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.