How to retrieve rows of a series object by regular expression in the pandas filter method?

PandasServer Side ProgrammingProgramming

By using the regex parameter we can apply the regular expression to the filter() method and this helps to retrieve the rows of the series object. The basic working of series.filter() method in pandas series constructor is used to subset the rows of a series object based on the index labels.

The parameter regex is used to define a search pattern (regular expression) that is used to retrieve the resultant rows.

Example 1

In this following example, we have created a series object using a list of integers and the index labels are created by using the pandas data range function.

# importing pandas package
import pandas as pd

# create date index
index = pd.date_range('2021-08-1', periods=10, freq='10H30min40s')

#creating pandas Series with date-time index
series = pd.Series([1,2,3,4,5,6,7,8,9,10], index=index)

print(series)

print("Output: ")
# Apply the filter method with regex
print(series.filter(regex='40$'))

Explanation

Here, we filter out some rows in the pandas series object by specifying the search pattern using the regex parameter.

Output

The output is given below −

2021-08-01 00:00:00    1
2021-08-01 10:30:40    2
2021-08-01 21:01:20    3
2021-08-02 07:32:00    4
2021-08-02 18:02:40    5
2021-08-03 04:33:20    6
2021-08-03 15:04:00    7
2021-08-04 01:34:40    8
2021-08-04 12:05:20    9
2021-08-04 22:36:00    10
Freq: 37840S, dtype: int64

Output:
2021-08-01 10:30:40    2
2021-08-02 18:02:40    5
2021-08-04 01:34:40    8
Freq: 113520S, dtype: int64

We can notice the above output block, we have successfully filtered the rows of the series object which is having 40 sec in the index.

Example 2

Let’s take another series object to filter out rows whose index label name has a space in it.

# importing pandas package
import pandas as pd

Countrys = ['Brazil','Canada','New Zealand','Iceland', 'India', 'Sri Lanka', 'United    States']
Capitals = [ 'Belmopan','Ottawa','Wellington','Reykjavik', 'New Delhi','Colombo',          'Washington D.C']

#creating pandas Series
series = pd.Series(Capitals, index=Countrys)

print(series)

print("Output: ")
# Apply the filter method with regex
print(series.filter(regex='. .'))

Output

The output is mentioned below −

Brazil                 Belmopan
Canada                   Ottawa
New Zealand          Wellington
Iceland               Reykjavik
India                 New Delhi
Sri Lanka               Colombo
United States    Washington D.C
dtype: object

Output:
New Zealand        Wellington
Sri Lanka             Colombo
United States  Washington D.C
dtype: object

In this above output, we have successfully filtered the few rows named “New Zealand”, “Sri Lanka”, “United States” from the series object.

raja
Updated on 07-Mar-2022 07:52:36

Advertisements