magres = np.sqrt(u * u + v * v)
magdir = np.rad2deg(np.arctan2(u, v))
if magdir < 180:
    magdir += 180
elif magdir > 180:
    magdir -= 180
return magdir

def MagRes(group):
    u = np.sum(group['MagViento'] * np.sin(np.deg2rad(group['DirViento'])))
v = np.sum(group['MagViento'] * np.cos(np.deg2rad(group['DirViento'])))
return np.sqrt(u * u + v * v)
How to add a new column to a hierarchical dataframe grouped by groupby