Pandas Series filter() Function

Pandas Series.filter() function is used to return the subset of values from Series that satisfies the condition. The filter() is applied with help of the index labels or on the values themselves. We can filter or subset the values of the pandas series using various functions.

In this article, I will explain filter() syntax, parameters, and how to filter the values or rows from the Pandas Series and also filtering values by using where(), isin(), loc[], and lambda functions.

1. Quick Examples of Series filter() Function

If you are in hurry below are some quick examples of the Pandas Series filter() function.


# Below are a quick example

# Example 1: use Series.filter() function to filter a pandas series
ser2 = ser.filter(regex = '. .')

# Example 2: filter() index by labels
ser2 = ser.filter(items = ['Spark', 'Python'])

# Example 3 : use loc[] and lambda to filter a pandas series
ser2 = ser.loc[lambda x : x == 23000]

# Example 4: use loc[] property & OR condition
ser2 = ser.loc[lambda x : (x  28000)]

# Example 5: use where() function to filter series
ser2 = ser.where(ser < 25000).dropna()

# Example 6: use isin() function to filter series
ser2 = ser[ser.isin([23000,28000])]

2. Syntax of Series.filter() Function

Following is the syntax of create Series.filter() function.


# Syntax of Series.filter() function
Series.filter(items=None, like=None, regex=None, axis=None)

2.1 Parameter of filter()

Following are the parameters of the filter().

  • items – Takes a list of axis labels that you wanted to filter.
  • like – Takes the axis string label that you wanted to filter.
  • regex – regular expression.
  • axis – {0 or ‘index’, 1 or ‘columns’, None}, default None. When not specified it used columns.

2.2 Return Value of filter()

It returns the value of the filter() the same type as the input object.

3. Create Pandas Series

Pandas Series is a one-dimensional, Index-labeled data structure that is available only in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.

Note : Series data structure is the same as the NumPy array data structure but only one difference that is arrays indices are integers and start with 0, whereas in series, the index can be anything even strings. The labels do not need to be unique but they must be of hashable type.

Now, let’s create pandas series using list of values.


import pandas as pd
  
# Create the Series
ser = pd.Series([20000,25000,23000,28000,55000,23000])
  
# Create the Index
index = ['Java','Spark','PySpark','Pandas','python NumPy','Python']
  
# set the index
ser.index = index
print(ser)

Yields below output.


# Output
Java            20000
Spark           25000
PySpark         23000
Pandas          28000
python NumPy    55000
Python          23000
dtype: int64

4. Use Series.filter() Function To Filter a Pandas Series

By using Series.filter() function you can filter the Series by index labels or by values. When you use index labels to files you can use regular expressions by using “regex”. The following example filters values from the given series object whose index label name has a space.


# Use Series.filter() function to filter a pandas series
ser2 = ser.filter(regex = '. .')
print(ser2)

Yields below output.


# Output
python NumPy    55000
dtype: int64

5. Filter Series by Index Labels

By default pandas.Series.filter() select the indexes by labels you specified using item, like, and regex parameters. The following example filters series with the list of index labels Spark and Python.


# filter() index by labels
ser2 = ser.filter(items = ['Spark', 'Python'])
print(ser2)

Yields below output.


# Output
Spark     25000
Python    23000
dtype: int64

6. Use loc[] & Lambda to Filter a Pandas Series

You can also filter the Pandas Series using Series.loc[] along with lambda function. The following example returns values from a series where values are equal to 23000.


# Use loc[] and lambda to filter a pandas series
ser2 = ser.loc[lambda x : x == 23000]
print(ser2)

Yields below output.


# Output
PySpark    23000
Python     23000
dtype: int64

Alternatively, you can also apply an “OR” condition with the “loc[]” property. The following example filters values that are less than 23000 or values greater than 28000. For examples.


# Use loc[] property & OR condition
ser2 = ser.loc[lambda x : (x < 23000 or x > 28000)]
print(ser2)

Yields below output.


# Output
Java            20000
python NumPy    55000
dtype: int64

7. Use where() Function To Filter Series

We can also use where() function to filter a series by values using expressions.


# Use where() function to filter series
ser2 = ser.where(ser < 25000).dropna()
print(ser2)

Yields below output.


# Output
Pandas          28000.0
python NumPy    55000.0
dtype: float64

8. Use isin() Function To Filter Series

By use isin() function is used to get the values from the series that are present in the list of values.


# Use isin() function to filter series
ser2 = ser[ser.isin([23000,28000])]
print(ser2)

Yields below output.


# Output
PySpark    23000
Pandas     28000
Python     23000
dtype: int64

9. Complete Example For Series filter() Function


import pandas as pd
  
# Create the Series
ser = pd.Series([20000,25000,23000,28000,55000,23000])
  
# Create the Index
index_ = ['Java','Spark','PySpark','Pandas','python NumPy','Python']
  
# set the index
ser.index = index_
print(ser)

# use Series.filter() function to filter a pandas series
ser2 = ser.filter(regex = '. .')
print(ser2)

# filter() index by labels
ser2 = ser.filter(items = ['Spark', 'Python'])
print(ser2)

# use loc[] and lambda to filter a pandas series
ser2 = ser.loc[lambda x : x == 23000]
print(ser2)

# use loc[] property & OR condition
ser2 = ser.loc[lambda x : (x  28000)]
print(ser2)

# use where() function to filter series
ser2 = ser.where(ser < 25000).dropna()
print(ser2)

# use isin() function to filter series
ser2 = ser[ser.isin([23000,28000])]
print(ser2)

10. Conclusion

In this article, you have learned the how to filter the Pandas Series by using filter(), where(), isin(), and loc[] with lambda function by using examples.

Happy Learning !!

You May Also Like

References

Leave a Reply

You are currently viewing Pandas Series filter() Function