Returning columns conditionally in pandas
Question: I've read quite a few questions and answers on using indexing with pandas in
python, but I can't work out how to return columns conditionally. For
instance, consider the following:

    
    
    import pandas as pd
    df = pd.DataFrame([[0,1,1],[0,0,1],[0,0,0]], columns=['a','b','c'])
    print(df)
    
       a  b  c
    0  0  1  1
    1  0  0  1
    2  0  0  0
    

How would I ask pandas to give me column names such that value in row zero and
that column is 1? Anticipated output: `['b','c']`

How would I ask pandas to give me column names such that value in row zero and
row one of that column is 1? Anticipated output: `['c']`

I'm comfortable using `.loc` to find all rows where specific conditions are
true, but this is slightly different.


Answer: @PadraicCunningham provided a solution in the comments. It was as simple as:

    
    
    df.columns[df.loc[0] == 1]
    

If I want to select only columns that meet two (or more criteria), that can be
listed in statements like the following:

    
    
    df.columns[(df.loc[:1] == 1).all()]
    df.columns[(df.loc[[0, 2]] == 1).all()]
    

The former returns columns in which all but the last row takes value 1. The
latter returns columns in which the first and third row (labeled 0 and 2) take
value 1. My error was trying to do this with `df.loc`.

