I feel like the answer to my question may be obvious, but I can't quite figure it out. I want to know the best way (or any good way), in python, to threshold a numerical variable such that the average of the values above this threshold (in my case, it happens to be above, but it could just as easily be below) is equal to a particular, given number. I would be happy with any efficient or efficient-ish solution using numpy or pandas.
Start with a pandas series (or a 1D numpy array) such as:
[0.1, 0.2, 0.3, 0.4, 0.5]
(in practice, the series or array may be very long). Suppose for instance that the given number, which is the target average, is 0.35. In this case, we can eyeball that the desired threshold must be any number greater than or equal to 0.1 but less than 0.2, since the average of 0.2, 0.3, 0.4, and 0.5 (all above the threshold) is equal to 0.35. (In particular, the answer isn't unique.)
Also, unlike the toy example above, in some cases it may be impossible to exactly match the given number. But I still want to solve for a threshold such that the average of all values above that threshold is as close to the given number as possible.
Any advice on how to accomplish this in Python is greatly appreciated. In particular, if there exists a numpy or pandas method that does this, please let me know. And if my question requires further clarification, please let me know. Thank you!