Home > Enterprise >  Map float ranges in pandas.series to strings in dictionary and save to dataframe
Map float ranges in pandas.series to strings in dictionary and save to dataframe

Time:12-16

I would like a simple method for mapping float ranges to strings and saving back to a dataframe. I understand you can not use range() with floats.

setup df

import pandas as pd
d = [1.1, 5.6, 7.9, 6.2]
df = pd.DataFrame(data=d)

Mapping dictionary or this could be an if else block if needs be, I prefer a dict as it is quicker to setup but happy to use if else should there be a simplar solution that way.

grade_dict = {
            (0, 4.9) :'average 4th-grade student or lower',
            (5.0, 5.9) :'average 5th or 6th-grade student',
            (6.0, 6.9) :'average 7th or 8th-grade student',
            (7.0, 7.9) :'average 9th or 10th-grade student',
            (8.0, 8.9) :'average 11th or 12th-grade student',
            (9.0, 9.9) :'average 13th to 15th-grade (college) student'}

Then I want to do something like this

df['dale_chall_readability_score'] = df['dale_chall_readability_score'].map(grade_dict)

I am thinking there is something I am missing as I am finding it tricky put together a working solution for this.

CodePudding user response:

try using apply where key[0] is indice 1 of the tuple range and key[1] is indice 2 of the tuple range

import pandas as pd
d = [1.1, 5.6, 7.9, 6.2]
df = pd.DataFrame(data=d)

grade_dict = {
            (0, 4.9) :'average 4th-grade student or lower',
            (5.0, 5.9) :'average 5th or 6th-grade student',
            (6.0, 6.9) :'average 7th or 8th-grade student',
            (7.0, 7.9) :'average 9th or 10th-grade student',
            (8.0, 8.9) :'average 11th or 12th-grade student',
            (9.0, 9.9) :'average 13th to 15th-grade (college) student'}
def grade_range(x):
    for key in grade_dict:
        if x >= key[0] and x <= key[1]:
            return grade_dict[key]

df['grade_range'] = df[0].apply(grade_range)

print(df)

output:

     0                         grade_range
0  1.1  average 4th-grade student or lower
1  5.6    average 5th or 6th-grade student
2  7.9   average 9th or 10th-grade student
3  6.2    average 7th or 8th-grade student
  • Related