Need a better way to handle hard-coded index values in a function definition

Hi DQ Community

I need help with the below code. Can it be done using a for loop or any other loop or by any other method?

The code currently has index-number and list hard-coded in the “if block” of the function “update_range” being defined.

The code should run for all the four values in the correct_range and it should replace corresponding values in the series. Retaining “null” and “other” values as is.

“Incorrect_range” list specifically created for “if block”. If this can be omitted completely the better.

Any ideas, please.

# correct the values which have month name instead of number range. For example, 01-Feb should be 1-2, Nov-20 should be 11-20

# declare series
trial_series = pd.Series(("1.0","1.0","2.0","3.0","01-Feb", "03-Apr", "05-Jun", "Nov-20","01-Feb", 
                         "03-Apr", "05-Jun", "Nov-20","01-Feb", "03-Apr", "05-Jun", "Nov-20",
                         "01-Feb", "03-Apr", "05-Jun", "Nov-20", "1.0","1.0","2.0","3.0", "1.0","1.0","2.0","3.0",
                         "6.0","7.0","8.0","7.0", "6.0","8.0","8.0","6.0", "6.0","7.0","8.0","7.0", np.nan, np.nan, np.nan))

# print the trial_series value count before update
print(trial_series.value_counts(dropna=False).sort_index(ascending=True))

incorrect_range = ["01-Feb", "03-Apr", "05-Jun", "Nov-20"]
correct_range = {"01-Feb":"1-2", "03-Apr":"3-4", "05-Jun":"5-6", "Nov-20":"11-20"}

def update_range(val):
        if val == incorrect_range[0]: #"01-Feb":
            return correct_range[val]
        elif val == incorrect_range[1]: #"03-Apr":
            return correct_range[val]
        elif val == incorrect_range[2]: #"05-Jun":
            return correct_range[val]
        elif val == incorrect_range[3]: #"Nov-20":
            return correct_range[val]
        elif pd.isnull(val):
            return np.nan
        else:
            return val
        
# use apply method to replace corresponding values in series
trial_result = trial_series.apply(update_range)

# print the trial_series value count after update
trial_result.value_counts(dropna=False).sort_values(ascending=False)

Thank you & regards

If I’m understanding what you want to do correctly, you can use the Series.replace() method. It can take in a dictionary as a parameter to replace the undesired values.

correct_range = {"01-Feb":"1-2", "03-Apr":"3-4", "05-Jun":"5-6", "Nov-20":"11-20"}
trial_result = trial_series.replace(correct_range)
2 Likes

Hi @april.g

Woah! the answer is so simple. I should have referred to the documentation of “replace” method. :open_mouth:
I guess I got more focused on how to do it by the “apply” method. :disappointed:

Thank you.

1 Like