0

I tried to extract a value range from a pandas column using loc but I fail with:

df.loc[0.5<df['a']<1.5, ['a']]

What is the correct pandas way to do this?

Henry Ecker
  • 31,792
  • 14
  • 29
  • 50
mati
  • 913
  • 4
  • 11
  • 16

1 Answers1

2

Use Series.between:

df.loc[df['a'].between(0.5, 1.5, inclusive=False), ['a']]

Or chain conditions with & for bitwise AND:

df.loc[(df['a'] > 0.5) & (df['a'] < 1.5), ['a']]

Or DataFrame.query with select a to one column DataFrame:

df[['a']].query("0.5 < a < 1.5")
#alternative
#df.query("0.5 < a < 1.5")[['a']]
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090