3

I know how to filter a dataframe by column value:

import pandas as pd
import numpy as np
from numpy.random import randn
np.random.seed(101)
df = pd.DataFrame(randn(5,4),index='A B C D E'.split(),columns='W X Y Z'.split())
print(df)
# show only rows where 'W' is positive
# here, the row for 'C' will be deleted, since df['W']['C']<0
df[df['W']>0]

But how do I filter by row value, e.g. 'B'>0?

Since df['X']['B']<=0 and df['Y']['B']<=0, I would like to delete columns X and Y. I tried the following code, but it reports an error:

df.loc[df.loc['B']>0]
2
  • "it reports an error" -- which one? Commented Nov 12, 2022 at 0:52
  • The question in other words: how to filter out only those columns of a pandas dataframe where the column's value in a chosen row meets a condition? Commented Jan 5, 2023 at 20:53

1 Answer 1

3

You should using the filter on columns

df.loc[:,df.loc['B',:]>0]
Out[67]: 
          W         Z
A  2.706850  0.503826
B  0.651118  0.605965
C -2.018168 -0.589001
D  0.188695  0.955057
E  0.190794  0.683509
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Shorter version: df.loc[:,df.loc['B']>0]

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.