Grabbing a value from a dataframe based on random variables created

Viewed 24

I have a dataframe with a specific value called rate_multiplier that I need to grab and compare it to the rate_multiplier I get from AWS s3 bucket.

In order to grab the rate_multiplier in the dataframe, I need to take the random variables that I created for a "person" and match them to the dataframe which gives a specific rate_mulitplier based on these certain characteristics

For example:

Random variables created:

Life_term = 25
Gender = F
Rate_class = Best-AB
Age = 27
Coverage = 2310

Dataframe:

        Life_term     Benefit_Length_months  Gender     Rate_class            Age   State    Coverage band (low end)    Coverage band (high end)    Coverage Band   Rate_multiplier
0       15            180                        M      Best-AA               18    Default  500                        1199                        500-1199        2.31
1       15            180                        M      Best-AA               19    Default  500                        1199                        500-1199        2.21
2       15            180                        M      Best-AA               20    Default  500                        1199                        500-1199        2.11
3       15            180                        M      Best-AA               21    Default  500                        1199                        500-1199        2.03
4       15            180                        M      Best-AA               22    Default  500                        1199                        500-1199        1.95
... ... ... ... ... ... ... ... ... ... ...
34987   10            120                        F      Nicotine Average-CD   61    Default  3600                       10000                       3600+           19.10
34988   10            120                        F      Nicotine Average-CD   62    Default  3600                       10000                       3600+           21.27
34989   10            120                        F      Nicotine Average-CD   63    Default  3600                       10000                       3600+           23.44 
34990   10            120                        F      Nicotine Average-CD   64    Default  3600                       10000                       3600+           25.61
34991   10            120                        F      Nicotine Average-CD   65    Default  3600                       10000                       3600+           27.78

So for this example, my randomly generated person would get a rate_multiplier of:

0.93

My code is as follows:

rate_mult_df.loc[(rate_mult_df['Life_term'] == 15) & (rate_mult_df['Gender'] == 'F') & (rate_mult_df['Rate_class'] == 'Best-AB') & (rate_mult_df['Age'] == 27) & (rate_mult_df['Coverage band (low end)'] <= 2310) & (rate_mult_df['Coverage band (high end)'] >= 2310)]

Is the right way to grab the rate_muliplier for the randomly genreated person or is there any easier way? Any and all help is appreciated. Please let me know if my question is clear enough. Working on that everyday.

1 Answers

For perfomance reasons I'd use .query()

rate_multiplier = df.query(
    "Life_term == 15 &"
    " Gender == 'F' &"
    " Rate_class == 'Best-AB' &"
    " Age == 27 &"
    " `Coverage band (low end)` == 2310 &"
    " `Coverage band (high end)` == 2310"
)["Rate_multiplier"].squeeze()

"Easier" depends on your workflow. For example if you want to query from a dictionary you could use:

def get_rate_multiplier(search_params: dict) -> str:
    return " and ".join(
            [f"({k} == '{v}')" if type(v) == str else f"({k} == {v})" for k, v in search_params.items()]
        )


random_person = {
    "Life_term": 15, "Gender": "F", "Rate_class": "Best-AB",
    "Age": 27, "Coverage band (low end)": 2310, "Coverage band (high end)": 2310
}
rate_multiplier = float(df.query(get_rate_multiplier(random_person))["Rate_multiplier"].squeeze())
Related