如何使用for循环在python中预测变量?

时间:2018-12-31 17:01:05

标签: python pandas for-loop

我有一个三列的pandas数据框,需要按如下所示在for循环中进行预测:X1 =上个月是Y,两个月前是X2 = Y,Y = 0.5 * X1 + 0.5 * X2

datetime      Y     X1     X2
11/15/2018  288.50 310.88 298.13 
12/15/2018  265.50 288.50 310.88 
1/15/2019   NaN    NaN    NaN
2/15/2019   NaN    NaN    NaN
3/15/2019   NaN    NaN    NaN
4/15/2019   NaN    NaN    NaN
5/15/2019   NaN    NaN    NaN

1 个答案:

答案 0 :(得分:0)

from numpy import NaN
import pandas as pd
pd.options.mode.chained_assignment = None
df = pd.DataFrame({'datetime':['11/15/2018','12/15/2018','1/15/2019','2/15/2019','3/15/2019','4/15/2019' ,'5/15/2019'], 'y':[ 288.50,265.50,NaN,NaN,NaN,NaN,NaN],'x1':[ 310.88, 288.50,NaN,NaN,NaN,NaN,NaN],'x2':[ 298.13,310.88,NaN,NaN,NaN,NaN,NaN]})
print(df)
df.x1=df.x1.fillna(method='ffill')
df.iloc[1::2].x2=df.iloc[1::2].x2.fillna(method='ffill')
df.iloc[::2].x2=df.iloc[::2].x2.fillna(method='ffill')
df.y=df.y.fillna(0.5*df.x1+0.5*df.x2) # or df.y=df.y.fillna(0.5*(df.x1+df.x2))
print(df)

输出

     datetime      x1      x2      y
0  11/15/2018  310.88  298.13  288.5
1  12/15/2018  288.50  310.88  265.5
2   1/15/2019     NaN     NaN    NaN
3   2/15/2019     NaN     NaN    NaN
4   3/15/2019     NaN     NaN    NaN
5   4/15/2019     NaN     NaN    NaN
6   5/15/2019     NaN     NaN    NaN

     datetime      x1      x2        y
0  11/15/2018  310.88  298.13  288.500
1  12/15/2018  288.50  310.88  265.500
2   1/15/2019  288.50  298.13  293.315
3   2/15/2019  288.50  310.88  299.690
4   3/15/2019  288.50  298.13  293.315
5   4/15/2019  288.50  310.88  299.690
6   5/15/2019  288.50  298.13  293.315