Skip to content
Advertisement

Python: for loop that drops a column to meet condition

I have a dataframe that looks as follows:

JavaScript

Beta is calculated as ((sum of each row)^2)/10. I want to keep dropping columns until Beta is less than or equal to 1 for all rows.

So far I have

JavaScript

How can I stop the loop when all values of beta are below or equal to 1?

Advertisement

Answer

First of all, if you want to calculate the power of a number, do not use ^ operator. It is an XOR Boolean operator. Instead, use ** operator.

This code should work. However, this will also delete last remaining column from the dataframe, if the condition of Beta is not met.

JavaScript

If you do not want the last remaining column to be deleted even if the Beta condition is not met, use this code

JavaScript
Advertisement