Skip to content Skip to sidebar Skip to footer

Pandas Between Range Lookup Filtering

My data looks like this: import pandas as pd pd.DataFrame({ 'x_range':['101-200','101-200','201-300','201-300'], 'y':[5,6,5,6], 'z': ['Cat', 'Dog', 'Fish', 'Snake']

Solution 1:

Simple filtering exercise:

Save your dataframe:

df['x_range_start'] = [int(i.split('-')[0]) for i in df.x_range]

Add two columns for range start and end:

df['x_range_start'] = [int(i.split('-')[0]) for i in df.x_range]
df['x_range_end'] = [int(i.split('-')[1]) for i in df.x_range]

Filter to find values:

x_value = 113
y_value = 5

df[(df.x_range_start <= x_value) &(x_value <= df.x_range_end)][df.y == y_value]['z']

Solution 2:

I don't love this, but I hacked together a solution:

Split:

df['low'], df['high'] = df['x_range'].str.split('-', 1).str
df['low'] = pd.to_numeric(df['low'])
df['high'] = pd.to_numeric(df['high'])

Filter:

x =248
y =6row= df[(pd.Series.between(x, left=df.low, right=df.high, inclusive=True)) & 
(df.y == y)]
row['z']

Post a Comment for "Pandas Between Range Lookup Filtering"