基于熊猫数据框的方程式求解

Suppose I have the dataframe x:

x = pd.DataFrame({
    'A': [5,4,3,2,1],
    'B': [3,2,3,2,3],
    'C': [2,3,4,2,3]
})

And column D defined as:

x['D'] = x.A*x.B-x.C*x.B

I want to solve for some constant to add to B such that x.D.mean() becomes equal to some target T. For example, in this case x.D.mean() equals 0.4. Let's say my T is 0, then I'd have to add -2(x['B'] = x.B-2) in order for x.D.mean() to equal 0. Can I solve this type of problem using pandas/numpy?

评论