2

Suppose I have a following dataframe:

x = pd.DataFrame(
    {
        'A': np.random.normal(0, 1, 100),
        'B': np.random.normal(0, 1, 100), 
        'C': np.random.normal(0, 1, 100), 
        'D': np.random.normal(0, 1, 100), 
        'E': np.random.normal(0, 1, 100) 
    }
)

I want to use loc and select only those rows where a value of certain is less than 0.5. I know I can do this as follows:

df.loc[df.A < 0.5, :]

and for multiple columns, I can do as follows:

df.loc[(df.A < 0.5) | (df.B < 0.5) | (df.C < 0.5), :]

My question is: Is there a better way to write conditions inside loc when you have more than 10 columns. I suppose I can do it the way I have shown above, but it becomes very tedious and I was hoping for a better way to do so.

tdy
  • 36,675
  • 19
  • 86
  • 83
monte
  • 1,482
  • 1
  • 10
  • 26

1 Answers1

3

You can use

df.loc[(df[['A', 'B', 'C']] < 0.5).any(axis=1)]
ozacha
  • 1,212
  • 9
  • 14