2

I'm trying to compare a value in a dataframe with key ranges in a dictionary and grab the corresponding dictionary value. Been looking at loop and iterations, however, as a beginner, can't get it working.

Example:

import pandas as pd

colors = {range(0,50):"red",range(50,100):"blue",
          range(100,150):"green",range(150,200):"orange"
          ,range(200, 250):"purple", range(250,1000):"grey"}

df = pd.DataFrame(data=[51,8,265,167], columns=['A'])

The result I'm looking for:

    A     color
0   51    blue
1   8     red
2   265   grey
3   167   orange

This is my basic idea (used a hard coded value):

for (start, end), value in colors.items():
    if start <= 85 < end:
        print(value) #add retrieved value to dataframe

I guess I could use a comprehension and/or a lambda function but again, I'm a beginner.

jpp
  • 159,742
  • 34
  • 281
  • 339
Rene
  • 1,095
  • 1
  • 8
  • 17

1 Answers1

2

You can vectorise your calculation using numpy.select. This will be more efficient, and easier to maintain, than a loopy if / else construct.

import pandas as pd, numpy as np

colors = {range(0,50):"red",range(50,100):"blue",
          range(100,150):"green",range(150,200):"orange",
          range(200, 250):"purple", range(250,1000):"grey"}

df = pd.DataFrame(data=[51,8,265,167], columns=['A'])

df['color'] = np.select([df['A'].isin(x) for x in colors], colors.values(), None)

Result

     A   color
0   51    blue
1    8     red
2  265    grey
3  167  orange
jpp
  • 159,742
  • 34
  • 281
  • 339