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.