Calculate depth and duration of exoplanet's transit using Python, astropy or lightkurve
Calculation of both, depth and duration, is usually done not on the raw data but derived from a fit to the data.

In your last three lines of code you also calculate the average / medium over all data while you should calculate the uneclipsed mean or median flux only for the non-transit time (with using median it possibly has only a tiny influence, yet it might).

As a first and crude step, I'd de-noise the data by applying a floating average filter over the data; you will have to test for its width and see what gives you best results: you don't want to average out features, but you want to average-out noise.

The better approach is to not smooth but actually fit a physical model to the data which takes into account the typical light curve behaviour of a transit. For an implementation I can point you to pytransit (reference paper). (Are you sure you are not re-inventing the wheel?). See also this paper by Maxted and Gill for a comparison of a few algorithms.