我有一个带有门槛和利润的df:
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)
有没有一种panda-way来通过找到过滤value1和value2的阈值的最佳拟合边际来优化total_profit?
我的意思是我可以遍历DF并增加/减少过滤器值,直到我找到最合适的值......但我猜有人已经这样做了......也许sci-py?
所以我基本上需要一个函数返回value1和value2的最佳拟合,所以我可以过滤我的DF并优化total_profit。假设是,value1,value2和profit之间存在相关性。
感谢和祝福, 即
答案 0 :(得分:2)
假设您只想使用df.Value1和df.Value2的观察值,以下内容将起作用。
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]