AlmostThere
AlmostThere

Reputation: 647

Filter Pandas Series and return values

I have the following series and I want to filter it down to between two values. When I use between, boolean logic is return. I want to filter the series to values between the two values and return those values.

# import pandas as pd
import pandas as pd
  
# Creating empty series
ser = pd.Series(range(1,10))

# between 3 and 7
ser = ser.between(3, 7)

This is what is returned:

0    False
1    False
2     True
3     True
4     True
5     True
6     True
7    False
8    False

But what I am looking for is the following:

2    3
3    4
4    5
5    6
6    7

Upvotes: 0

Views: 50

Answers (1)

imdevskp
imdevskp

Reputation: 2223

use

# import pandas as pd
import pandas as pd
  
# Creating empty series
ser = pd.Series(range(1,10))

# between 3 and 7
print(ser[ser.between(3, 7)])

Upvotes: 2

Related Questions