How to efficiently use bin values ​​in overlapping cells using Pandas?

I would like to remove all values ​​from a float column into cells that overlap. The resulting column can be a series of 1-D vectors with bools - one vector for each value from the original column. The resulting vectors contain True

for each bin, the value of which falls False

for other boxes as well.

For example, if I have four cells [(0, 10), (7, 20), (15, 30), (30, 60)]

and the original value is 9.5, the resulting vector should be [True, True, False, False]

.

I know how to iterate over all ranges using a custom function using "apply", but is there a way to do this binning more efficiently and concisely?

+3


source to share


1 answer


Will a simple checklist check suit your needs?

Bins = [(0, 10), (7, 20), (15, 30), (30, 60)]
Result = [((9.5>=y[0])&(9.5<=y[1])) for y in Bins]

      

If your data is stored in a data

pandas DataFrame ( df

) column , you can define a function:



def in_ranges(x,bins):
    return [((x>=y[0])&(x<=y[1])) for y in bins]

      

and apply it to the column:

df[data].apply(lambda x: pd.Series(in_ranges(x,Bins),Bins))

      

+2


source







All Articles