0

im quite new to python. i just wanna know what is the best way to select certain columns based on values on a row.

c = pd.DataFrame(np.arange(12).reshape(4,3))
c
Out[1]: 
   0   1   2
0  0   1   2
1  3   4   5
2  6   7   8
3  9  10  11

say i want to select the columns whose second row's value is larger than 3.

It seems c.loc[:,c.loc[1]>3] works for me. Is it the most efficient way? or any other way to achieve this?

1 Answer 1

1

Is it the most efficient way?

Yes.

any other way to achieve this?

c.reindex(c.columns[c.loc[1] > 3], axis=1)

    1   2
0   1   2
1   4   5
2   7   8
3  10  11

Arguably not as good.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.