Use mask created with df > 2 with any and then select columns by ix:
import pandas as pd
np.random.seed(18)
df = pd.DataFrame(np.random.randn(2, 4))
print(df)
0 1 2 3
0 0.079428 2.190202 -0.134892 0.160518
1 0.442698 0.623391 1.008903 0.394249
print ((df>2).any())
0 False
1 True
2 False
3 False
dtype: bool
print (df.ix[:, (df>2).any()])
1
0 2.190202
1 0.623391
EDIT by comment:
You can check your solution per partes:
It seems it works, but it always select second column (1, python count from 0) column if condition True:
print (df.iloc[(0,1)])
2.19020235741
print (df.iloc[(0,1)] > 2)
True
print (df.columns[df.iloc[(0,1)]>2])
1
print (df[df.columns[df.iloc[(0,1)]>2]])
0 2.190202
1 0.623391
Name: 1, dtype: float64
And first column (0) column if False, because boolean True and False are casted to 1 and 0:
np.random.seed(15)
df = pd.DataFrame(np.random.randn(2, 4))
print (df)
0 1 2 3
0 -0.312328 0.339285 -0.155909 -0.501790
1 0.235569 -1.763605 -1.095862 -1.087766
print (df.iloc[(0,1)])
0.339284706046
print (df.iloc[(0,1)] > 2)
False
print (df.columns[df.iloc[(0,1)]>2])
0
print (df[df.columns[df.iloc[(0,1)]>2]])
0 -0.312328
1 0.235569
Name: 0, dtype: float64
If change column names:
np.random.seed(15)
df = pd.DataFrame(np.random.randn(2, 4))
df.columns = ['a','b','c','d']
print (df)
a b c d
0 -0.312328 0.339285 -0.155909 -0.501790
1 0.235569 -1.763605 -1.095862 -1.087766
print (df.iloc[(0,1)] > 2)
False
print (df[df.columns[df.iloc[(0,1)]>2]])
0 -0.312328
1 0.235569
Name: a, dtype: float64