Recover data set:
import pandas as pd data1 = [{'code':100}, {'code':120}, {'code':113}] data2 = [{'category':1, 'l_bound':99, 'r_bound':105}, {'category':2, 'l_bound':107, 'r_bound':110}, {'category':3, 'l_bound':117, 'r_bound':135}] data1 = pd.DataFrame(data1) data2 = pd.DataFrame(data2)
@ cแดสแด
sแดแดแดแด
answer ( preferred ), follow the double link:
idx = pd.IntervalIndex.from_arrays(data2['l_bound'], data2['r_bound'], closed='both') category = data2.loc[idx.get_indexer(data1.code), 'category'] data1['category'] = category.values
Here is a different approach. Create a map with a value in the range and categories.
# Create a map d = {i:k for k,v in data2.set_index('category').to_dict('i').items() for i in range(v['l_bound'],v['r_bound']+1)}
Finally
print(data1)
Return:
code category 0 100 1.0 1 120 3.0 2 113 NaN
If you want an int, we can do this:
data1.code.map(d).fillna(-1).astype(int) # -1 meaning no match
And we get:
code category 0 100 1 1 120 3 2 113 -1
source share