Bruno
Bruno

Reputation: 97

Filter rows from a pandas column binned by pandas.cut()

I have a pandas series that I've got from pandas.cut().

Given a value 'VALUE' I'd like a boolean series for all the rows whose interval comprises the given value.

For instance, if i have a value = 10 I'd like the rows with the bin (8, 12] to assume True and those with the bin (0, 8] assume False.

I´ve managed to do that using:

mask = (df['COLUMN'].apply(lambda x: x.left).astype('float64') <= VALUE ) & (df['COLUMN'].apply(lambda x: x.right).astype('float64') >= VALUE )

I feel this is not an efficient way for doing it. Is there any better way for doing so?

Upvotes: 3

Views: 1237

Answers (1)

BENY
BENY

Reputation: 323226

Yes there is one short cut

m=pd.arrays.IntervalArray(df['COLUMN']).overlaps(pd.Interval(VALUE, VALUE))

Or

m=pd.Index(df['COLUMN']).isin([VALUE])

Upvotes: 4

Related Questions