How do I forecast variables in python using for loops?
I have a pandas data frame with three columns and need to forecast them in a for loop as follows: X1 = Y prior month, X2 = Y two months ago and 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
python pandas for-loop
add a comment |
I have a pandas data frame with three columns and need to forecast them in a for loop as follows: X1 = Y prior month, X2 = Y two months ago and 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
python pandas for-loop
How do I add the data to this question?
– Jakob J Krummenacher
Dec 31 '18 at 17:02
Edit the question? Please add data in a way it can be copied, or reproduced, using a text editor.
– Daniel Mesejo
Dec 31 '18 at 17:06
I don't know how to paste data. THIS IS FRUSTRATING!
– Jakob J Krummenacher
Dec 31 '18 at 17:11
I give up. Tables do not paste well in Stack Overflow
– Jakob J Krummenacher
Dec 31 '18 at 17:17
Could it be pd.shift() that you want? As far as I understand, you want to lag each variable/regressor? for X1 you can lag the series withY.shift(1)
and for X2 you can do Y.shift(2) and then loop through each set of values afterwards
– Sharu
Dec 31 '18 at 19:34
add a comment |
I have a pandas data frame with three columns and need to forecast them in a for loop as follows: X1 = Y prior month, X2 = Y two months ago and 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
python pandas for-loop
I have a pandas data frame with three columns and need to forecast them in a for loop as follows: X1 = Y prior month, X2 = Y two months ago and 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
python pandas for-loop
python pandas for-loop
edited Dec 31 '18 at 17:27
Bitto Bennichan
2,5561220
2,5561220
asked Dec 31 '18 at 17:01
Jakob J KrummenacherJakob J Krummenacher
12
12
How do I add the data to this question?
– Jakob J Krummenacher
Dec 31 '18 at 17:02
Edit the question? Please add data in a way it can be copied, or reproduced, using a text editor.
– Daniel Mesejo
Dec 31 '18 at 17:06
I don't know how to paste data. THIS IS FRUSTRATING!
– Jakob J Krummenacher
Dec 31 '18 at 17:11
I give up. Tables do not paste well in Stack Overflow
– Jakob J Krummenacher
Dec 31 '18 at 17:17
Could it be pd.shift() that you want? As far as I understand, you want to lag each variable/regressor? for X1 you can lag the series withY.shift(1)
and for X2 you can do Y.shift(2) and then loop through each set of values afterwards
– Sharu
Dec 31 '18 at 19:34
add a comment |
How do I add the data to this question?
– Jakob J Krummenacher
Dec 31 '18 at 17:02
Edit the question? Please add data in a way it can be copied, or reproduced, using a text editor.
– Daniel Mesejo
Dec 31 '18 at 17:06
I don't know how to paste data. THIS IS FRUSTRATING!
– Jakob J Krummenacher
Dec 31 '18 at 17:11
I give up. Tables do not paste well in Stack Overflow
– Jakob J Krummenacher
Dec 31 '18 at 17:17
Could it be pd.shift() that you want? As far as I understand, you want to lag each variable/regressor? for X1 you can lag the series withY.shift(1)
and for X2 you can do Y.shift(2) and then loop through each set of values afterwards
– Sharu
Dec 31 '18 at 19:34
How do I add the data to this question?
– Jakob J Krummenacher
Dec 31 '18 at 17:02
How do I add the data to this question?
– Jakob J Krummenacher
Dec 31 '18 at 17:02
Edit the question? Please add data in a way it can be copied, or reproduced, using a text editor.
– Daniel Mesejo
Dec 31 '18 at 17:06
Edit the question? Please add data in a way it can be copied, or reproduced, using a text editor.
– Daniel Mesejo
Dec 31 '18 at 17:06
I don't know how to paste data. THIS IS FRUSTRATING!
– Jakob J Krummenacher
Dec 31 '18 at 17:11
I don't know how to paste data. THIS IS FRUSTRATING!
– Jakob J Krummenacher
Dec 31 '18 at 17:11
I give up. Tables do not paste well in Stack Overflow
– Jakob J Krummenacher
Dec 31 '18 at 17:17
I give up. Tables do not paste well in Stack Overflow
– Jakob J Krummenacher
Dec 31 '18 at 17:17
Could it be pd.shift() that you want? As far as I understand, you want to lag each variable/regressor? for X1 you can lag the series with
Y.shift(1)
and for X2 you can do Y.shift(2) and then loop through each set of values afterwards– Sharu
Dec 31 '18 at 19:34
Could it be pd.shift() that you want? As far as I understand, you want to lag each variable/regressor? for X1 you can lag the series with
Y.shift(1)
and for X2 you can do Y.shift(2) and then loop through each set of values afterwards– Sharu
Dec 31 '18 at 19:34
add a comment |
1 Answer
1
active
oldest
votes
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)
Output
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53989741%2fhow-do-i-forecast-variables-in-python-using-for-loops%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
Output
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
add a comment |
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)
Output
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
add a comment |
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)
Output
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
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)
Output
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
answered Jan 1 at 0:12
Bitto BennichanBitto Bennichan
2,5561220
2,5561220
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53989741%2fhow-do-i-forecast-variables-in-python-using-for-loops%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How do I add the data to this question?
– Jakob J Krummenacher
Dec 31 '18 at 17:02
Edit the question? Please add data in a way it can be copied, or reproduced, using a text editor.
– Daniel Mesejo
Dec 31 '18 at 17:06
I don't know how to paste data. THIS IS FRUSTRATING!
– Jakob J Krummenacher
Dec 31 '18 at 17:11
I give up. Tables do not paste well in Stack Overflow
– Jakob J Krummenacher
Dec 31 '18 at 17:17
Could it be pd.shift() that you want? As far as I understand, you want to lag each variable/regressor? for X1 you can lag the series with
Y.shift(1)
and for X2 you can do Y.shift(2) and then loop through each set of values afterwards– Sharu
Dec 31 '18 at 19:34