Pandas python optimize profit, linear optimization

I have a df with income and profit:

import numpy as np import pandas as pd

dates = pd.date_range('20130101',periods=6) 
df = pd.DataFrame(np.random.randn(6,3),index=dates,columns=['Value1', 'Value2', 'Profit']) 
df['Profit'] = df['Profit']*100 


print(df.to_string())

total_profit = df['Profit'].loc[(df.Value1 > 0) & (df.Value2 >= 0)].sum()

print(total_profit)

      

does panda -way exist to optimize total_profit by finding the best matching margins to hijack filter value1 and value2?

I mean, I could iterate over the DF and increment / decrement the filter values ​​until I find the best fit ... but I think someone has already done this ... sci-py perhaps?

so I basically want a function that returns the best possible options for value1 and value2, so I can filter my DF and optimize total_profit. it is assumed that there is a correlation between value1, value2 and profit.

thanks and best wishes, e.

+3


source to share


1 answer


Assuming you only want to use observable values ​​for df.Value1 and df.Value2 the following will work.



import numpy as np 
import pandas as pd

dates = pd.date_range('20130101',periods=6) 
df = pd.DataFrame(np.random.randn(6,3),index=dates,columns=['Value1', 'Value2', 'Profit']) 
df['Profit'] = df['Profit']*100 

print(df.to_string())

# create list of all possible value pairs
vals = [[i,j] for i in df.Value1 for j in df.Value2]

# create list of profits from all possible value pairs
total_profit = [df['Profit'].loc[(df.Value1 > i) & (df.Value2 >= j)].sum() for i, j in vals]

# get index of maximum profit
max_index = total_profit.index(max(total_profit))

# get values that correspond to max profit
vals[max_index]

Out[9]: [-0.51914224014959032, -0.73918945103973344]

      

+2


source







All Articles