# 根据特定条件和输入字典生成数据框-熊猫

``````d1 = { 'start_date' : '2020-10-01T20:00:00.000Z',
'end_date'  : '2020-10-05T20:00:00.000Z',
'n_days'    : 6,
'type'      : 'linear',
"coef": [0.1,0.1,0.1,0.1,0.1,0.1]
}
``````

``````Date                Day           function_type         function_value
2020-10-01          1             linear                (0.1*1)+0.1 = 0.2
2020-10-02          2             linear                (0.1*2)+0.1 = 0.3
2020-10-03          3             linear                (0.1*3)+0.1 = 0.4
2020-10-04          4             linear                (0.1*4)+0.1 = 0.5
2020-10-05          5             linear                (0.1*5)+0.1 = 0.6
``````

`type`可以是直链，恒定的，多项式和指数的。

``````a0, a1, a2, a3, a4, a5 = d1['coef']

If constant:
funtion_value = a0

If exponential:
funtion_value = e**(a0+a1T)

if polynomial:
funtion_value = a0+a1T+a2(T**2)+a3(T**3)+a4(T**4)+a5(T**5)

T: value of Day column
``````
Shubham Sharma

``````def funcValue(d, T):
a0, a1, a2, a3, a4, a5 = d['coef']
func = {
'constant': a0,
'linear': a0 + a1*T,
'polynomial': a0 + a1*T + a2*(T**2)+ a3 * (T**3) + a4*(T**4) + a5*(T**5),
'exponential':  np.power(np.e, a0 + a1*T)
}

return func[d['type']]
``````

``````def getDF(d):
date = pd.date_range(d['start_date'], d['end_date'], freq='D').tz_localize(None).floor('D')
days = (date - date[0]).days + 1
return pd.DataFrame({'Date': date, 'Day': days, 'function_type': d['type'],
'function_value': funcValue(d, days)})
``````

``````print(getDF(d1))

Date  Day function_type  function_value
0 2020-10-01    1        linear             0.2
1 2020-10-02    2        linear             0.3
2 2020-10-03    3        linear             0.4
3 2020-10-04    4        linear             0.5
4 2020-10-05    5        linear             0.6
``````

0 条评论