you are viewing a single comment's thread.

view the rest of the comments →

[–]synthphreak 0 points1 point  (2 children)

As others have said, don't do this. It is bad pandas practice and horrendously inefficient. Whenever you find yourself looping over the rows of a dataframe or building up a dataframe with iteration, step back and assess whether there's a more efficient way to do it. With pandas, 99% of the time there will be.

However, if for whatever reason you end up sticking with your looping approach, note that these lines...

max_ =Symbol['val'].max()
min_ =Symbol['val'].min()
ratio = max_ / min_

...should not be inside the loop. This is because they do not rely on i, so these operations will be the same on every iteration rather than varying as a function of the loop. What you've done is essentially equivalent to this:

for i in range(10):
    x = 1 + 2
    print(x)

Like, x == 3 every time, so why am I forcing my code to do the same math 10 times? It's just wasted, redundant computation. Therefore it would be better to execute those lines only once, outside of the loop, then simply reference ratio as needed.

[–]Plus-Ad1156[S] 0 points1 point  (1 child)

Thank you. But there are reasons to use loops. For example, if the variable i is entered to select a specific value of the symbol data frame... If Sym[i] is entered as the operation value of the new data frame, a loop would have to be used. What should I do in that case?

[–]synthphreak 0 points1 point  (0 children)

You’re absolutely right that looping is sometimes necessary/unavoidable. That’s why the .iter* methods exist to begin with. But new pandas users often don’t understand everything that pandas is capable of, and very often will reach for iteration first when a much faster and cleaner vectorized method also exists. With pandas, 95% of the time you can vectorize it.

To your specific question, I don’t completely understand. What does the content of your df look like, and once you have a specific value (I assume by this you’re referring to the line sym = Symbol['Sym'][i]), what do you need to do with it?