Оптимизируй код:

def calc_rsd(iterations, first_date, df, step_size):

    first_date = pd.to_datetime(first_date)
    rsd_df = pd.DataFrame()
    for iteration in range(1, iterations + 1):
        #выделяю даты
        dt = df[(df['msk_created_dttm'] >= first_date + pd.Timedelta(days=step_size * (iteration - 1)))
                                  & (df['msk_created_dttm'] < first_date + pd.Timedelta(days=step_size * iteration))].copy(deep=True).reset_index(drop=True)

        before_df = df[
            (df['msk_created_dttm'] >= first_date)
            & (df['msk_created_dttm'] < first_date + pd.Timedelta(days=step_size))
        ]
        
        date_start = first_date + timedelta(days=step_size * (iteration - 1))
        date_end = first_date + timedelta(days=step_size * iteration) - timedelta(days=1)

        #all mpu
        rsd_before = round(before_df['mpu'].std() / before_df['mpu'].mean(), 4) * 100
        rsd = round(dt['mpu'].std() / dt['mpu'].mean(), 4) * 100
        
        #without coef
        rsd_before_wo_coef = round(before_df['mpu_wo_coef'].std() / before_df['mpu_wo_coef'].mean(), 4) * 100
        rsd_wo_coef = round(dt['mpu_wo_coef'].std() / dt['mpu_wo_coef'].mean(), 4) * 100
        
        #with avg coef
        rsd_before_with_avg_coef = round(before_df['mpu_with_avg_coef'].std() / before_df['mpu_with_avg_coef'].mean(), 4) * 100
        rsd_with_avg_coef = round(dt['mpu_with_avg_coef'].std() / dt['mpu_with_avg_coef'].mean(), 4) * 100
        
        #with avg speed
        rsd_before_wo_speed = round(before_df['mpu_wo_speed_part'].std() / before_df['mpu_wo_speed_part'].mean(), 4) * 100
        rsd_wo_speed = round(dt['mpu_wo_speed_part'].std() / dt['mpu_wo_speed_part'].mean(), 4) * 100
        
        #with avg transfer
        rsd_before_wo_transfer = round(before_df['mpu_wo_transfer_part'].std() / before_df['mpu_wo_transfer_part'].mean(), 4) * 100
        rsd_wo_transfer = round(dt['mpu_wo_transfer_part'].std() / dt['mpu_wo_transfer_part'].mean(), 4) * 100
        
        #with new add km
        rsd_before_wo_add_km = round(before_df['mpu_wo_coef_wo_add_km'].std() / before_df['mpu_wo_coef_wo_add_km'].mean(), 4) * 100
        rsd_wo_add_km = round(dt['mpu_wo_coef_wo_add_km'].std() / dt['mpu_wo_coef_wo_add_km'].mean(), 4) * 100
        
        #with new add km with avg speed fact cte
        rsd_before_wo_add_km_with_avg_speed_fact_cte = round(before_df['mpu_wo_coef_wo_add_km_with_avg_speed_fact_cte'].std() / before_df['mpu_wo_coef_wo_add_km_with_avg_speed_fact_cte'].mean(), 4) * 100
        rsd_wo_add_km_with_avg_speed_fact_cte = round(dt['mpu_wo_coef_wo_add_km_with_avg_speed_fact_cte'].std() / dt['mpu_wo_coef_wo_add_km_with_avg_speed_fact_cte'].mean(), 4) * 100
        
        #with new add km with avg speed
        rsd_before_wo_add_km_with_avg_speed = round(before_df['mpu_wo_coef_wo_add_km_with_avg_speed'].std() / before_df['mpu_wo_coef_wo_add_km_with_avg_speed'].mean(), 4) * 100
        rsd_wo_add_km_with_avg_speed = round(dt['mpu_wo_coef_wo_add_km_with_avg_speed'].std() / dt['mpu_wo_coef_wo_add_km_with_avg_speed'].mean(), 4) * 100
    
        
        #without cap
        rsd_before_wo_cap = round(before_df['mpu_wo_cap'].std() / before_df['mpu_wo_cap'].mean(), 4) * 100
        rsd_wo_cap = round(dt['mpu_wo_cap'].std() / dt['mpu_wo_cap'].mean(), 4) * 100
        
        #without cap without coef
        rsd_before_wo_cap_wo_coef = round(before_df['mpu_wo_cap_wo_coef'].std() / before_df['mpu_wo_cap_wo_coef'].mean(), 4) * 100
        rsd_wo_cap_wo_coef = round(dt['mpu_wo_cap_wo_coef'].std() / dt['mpu_wo_cap_wo_coef'].mean(), 4) * 100
        
        #coeff
        coeff = round(dt['coeff'].mean(), 2)
        coeff_rsd = round(dt['coeff'].std() / dt['coeff'].mean(), 2)
        coeff_bb = round(dt['courier_demand_coefficient'].mean(), 2)
        coeff_bb_rsd = round(dt['courier_demand_coefficient'].std() / dt['courier_demand_coefficient'].mean(), 2)
        coeff_shift = round(dt['shift_additional_coeff'].mean(), 2)
        
        #output
        tmp = pd.DataFrame({
            'dates': [f'{date_start.date()} - {date_end.date()}'], 
            
            'RSD before': [rsd_before],
            'RSD': [rsd],
            'RSD diff': [rsd - rsd_before],
            
            'RSD before wo coef': [rsd_before_wo_coef],
            'RSD wo coef': [rsd_wo_coef],
            'RSD wo coef diff': [rsd_wo_coef - rsd_before_wo_coef],
            
            'RSD before with avg coef': [rsd_before_with_avg_coef],
            'RSD with avg coef': [rsd_with_avg_coef],
            'RSD with avg coef diff': [rsd_with_avg_coef - rsd_before_with_avg_coef],
            
            'coeff': [coeff],
            'coeff_rsd': [coeff_rsd],
            'coeff_bb': [coeff_bb],
            'coeff_bb_rsd': [coeff_bb_rsd],
            'coeff_shift': [coeff_shift],
            
            'RSD before with avg speed': [rsd_before_wo_speed],
            'RSD with avg speed': [rsd_wo_speed],
            'RSD with avg speed diff': [rsd_wo_speed - rsd_before_wo_speed],
            
            'RSD before with avg drop off': [rsd_before_wo_transfer],
            'RSD with avg drop off': [rsd_wo_transfer],
            'RSD with avg drop off diff': [rsd_wo_transfer - rsd_before_wo_transfer],
            
            'RSD wo coef before with new add km': [rsd_before_wo_add_km],
            'RSD wo coef with new add km': [rsd_wo_add_km],
            'RSD wo coef with new add km diff': [rsd_wo_add_km - rsd_before_wo_add_km],
            
            'RSD wo coef before with new add km with avg speed fact cte': [rsd_before_wo_add_km_with_avg_speed_fact_cte],
            'RSD wo coef with new add km with avg speed fact cte': [rsd_wo_add_km_with_avg_speed_fact_cte],
            'RSD wo coef with new add km with avg speed fact cte diff': [rsd_wo_add_km_with_avg_speed_fact_cte - rsd_before_wo_add_km_with_avg_speed_fact_cte],
            
            'RSD wo coef before with new add km with avg speed': [rsd_before_wo_add_km_with_avg_speed],
            'RSD wo coef with new add km with avg speed': [rsd_wo_add_km_with_avg_speed],
            'RSD wo coef with new add km with avg speed diff': [rsd_wo_add_km_with_avg_speed - rsd_before_wo_add_km_with_avg_speed],
            
            'RSD before wo cap': [rsd_before_wo_cap],
            'RSD wo cap': [rsd_wo_cap],
            'RSD wo cap diff': [rsd_wo_cap - rsd_before_wo_cap],
            
            'RSD before wo cap wo coef': [rsd_before_wo_cap_wo_coef],
            'RSD wo cap wo coef': [rsd_wo_cap_wo_coef],
            'RSD wo cap wo coef diff': [rsd_wo_cap_wo_coef - rsd_before_wo_cap_wo_coef]
        })
        rsd_df = pd.concat([rsd_df, tmp])


    return rsd_df