Pandas Series Chaining: Filter on boolean value

Viewed 1671

How can I filter a pandas series based on boolean values?

Currently I have:

s.apply(lambda x: myfunc(x, myparam).where(lambda x: x).dropna()

What I want is only keep entries where myfunc returns true.myfunc is complex function using 3rd party code and operates only on individual elements.

How can i make this more understandable?

2 Answers

You can understand it with below given sample code

import pandas as pd

data = pd.Series([1,12,15,3,5,3,6,9,10,5])
print(data)

# filter data based on a condition keep only rows which are multiple of 3
filter_cond = data.apply(lambda x:x%3==0)
print(filter_cond)
filter_data = data[filter_cond]
print(filter_data)

This code is about to filter the series data which are of the multiples of 3. To do that, we just put the filter condition and apply it on the series data. You can verify it with below generated output.

The sample series data:

0     1
1    12
2    15
3     3
4     5
5     3
6     6
7     9
8    10
9     5
dtype: int64

The conditional filter output:

0    False
1     True
2     True
3     True
4    False
5     True
6     True
7     True
8    False
9    False
dtype: bool

The final required filter data:

1    12
2    15
3     3
5     3
6     6
7     9
dtype: int64

Hope, this helps you to understand that how we can apply conditional filters on the series data.

Use boolean indexing:

mask = s.apply(lambda x: myfunc(x, myparam))
print (s[mask])

If also is changed index values in mask filter by 1d array:

#pandas 0.24+
print (s[mask.to_numpy()])

#pandas below
print (s[mask.values])

EDIT:

s = pd.Series([1,2,3])

def myfunc(x, n):
    return x > n

myparam = 1
a = s[s.apply(lambda x: myfunc(x, myparam))]
print (a)
1    2
2    3
dtype: int64

Solution with callable is possible, but a bit overcomplicated in my opinion:

a = s.loc[lambda s: s.apply(lambda x: myfunc(x, myparam))]
print (a)
1    2
2    3
dtype: int64
Related