擅长:python、mysql、java
<pre><code>import math
slopes = pd.Series({ -math.inf: 10, -200: 60, 0: 0, 200: 1, 300: 10, math.inf: 10})
</code></pre>
<p>根据需要定义成本函数</p>
<pre><code>def cost_func(x):
if x[0] < 200:
return abs(x[0]) * x[1]
else:
return 200 + abs(x[0]-200) * x[1]
slope_df = pd.DataFrame({'value':slopes.index, 'amount':slopes.values}, index = range(slopes.size))
slope_df['cost'] = slope_df[ ['value', 'amount'] ].apply(lambda row: cost_func(row), axis=1)
print(slope_df)
</code></pre>
<p><a href="https://i.stack.imgur.com/4jsv3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4jsv3.png" alt="slope_df output"/></a></p>