Naive prediction using pandas

Asked by At

Suppose, I have a data set:

ix    m_t1   m_t2
1     42     84
2     12     12
3     100    50

then, we can use

df = df[['m_t1', 'm_t2']].pct_change(axis=1).mul(100)[1]

to calculate the difference between m_t1 and m_t2 in %

like

diff
100
0
-50

I would like to apply this difference on m_t2 to get m_t3_predicted

m_t3_predicted
168
12
25

How can I do it?

P.S. Is there any name for the algorithm?

1 Answers

1
Klemen Koleša On Best Solutions

Try this:

df_diff=df[['m_t1', 'm_t2']].pct_change(axis=1).mul(100).drop(columns=["m_t1"])
df_diff

    diff
0   100.0
1   0.0
2   -50.0

Rename column in df_diff:

df_diff.columns=["diff"]

Concat dataframes:

df_result=pd.concat([df,df_diff],axis=1)

Then calculate:

df_result["m_t3_predicted"]=df_result["m_t2"]+df_result["diff"]/100*df_result["m_t2"]

Result:

    ix  m_t1    m_t2    diff    m_t3_predicted
0   1   42      84      100.0   168.0
1   2   12      12      0.0     12.0
2   3   100     50     -50.0    25.0