If all of the values ​in the column are outside of a certain range, drop that column

data sample:

x y
12 132
15 42
14 51
18 41
29 100
21 132

Suppose I have a condition like: If all values ​​in a column are not between 10 and 40, drop that column.

According to this condition, I have to drop the y column in the data sample. tried to use:

df.loc[:, (df >9 & df <40).any(axis=0)]

but I am getting this error:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Answer

You can use brackets in the condition else it’s parsed differently by the interpreter. And you have to use df.all.

df.loc[:, (df.gt(9) & df.lt(40)).all()]
# df.loc[:, ((df > 9) & (df < 40)).all(axis=0)] same as the above

   x
0  12
1  15
2  14
3  18
4  29
5  21