python – optimizing some lines of code in pandas

I'm trying to optimize the following lines after doing the profile, I found that they are slow, I have no experience with optimization, I hope someone helps

calculating the square root for the calculation of the distance

_df['ds'] = np.sqrt (np.array (_df['dx'] ** 2 + _df['dy'] ** 2, dtype = np.float32))


def resta_nmin (x):
returns x - np.nanmin (x)


_df['s'] = (_df.groupby (& # 39; trajectory_id & # 39;)['s']
             .transform (subtract_nanmin))
def divide_nanmax (x):
returns x / np.nanmax (x)

_df['s_normalized'] = (_df.groupby (& # 39; trajectory_id & # 39;)['s']
                            .transform (divide_nanmax))