Shifted results in pandas rolling mean












0















In the (5 first rows) result below, you can see Freq column and the rolling means (3) column MMeans calculated using pandas:



    Freq      MMeans
0 215 NaN
1 453 NaN
2 277 315.000000
3 38 256.000000
4 1 105.333333


I was expecting MMeans to start at index 1 since 1 is the mean of (0-1-2). Is there an option that I am missing with rolling method?



edit 1



print(pd.DataFrame({
'Freq':eff,
'MMeans': dF['Freq'].rolling(3).mean()}))


edit 2
Sorry @Yuca for not being as clear as I'd like to. Next is the columns I'd like pandas to return :



    Freq      MMeans
0 215 NaN
1 453 315.000000
2 277 256.000000
3 38 105.333333
4 1 29.666667


which are not the results returned with min_periods=2










share|improve this question

























  • without looking at your syntax it is difficult to know what you did. what value did you use for min_periods?

    – Ken Dekalb
    Nov 20 '18 at 21:10











  • how can you get 29 on the last row? also, by using the same logic as for the star of you df, your last row should be NAN

    – Yuca
    Nov 30 '18 at 16:56













  • The list I gave was a partial one, the complete is much more longer. Here is a link with a little longer view of the list and my code to produce what I was expecting.

    – david
    Nov 30 '18 at 17:42
















0















In the (5 first rows) result below, you can see Freq column and the rolling means (3) column MMeans calculated using pandas:



    Freq      MMeans
0 215 NaN
1 453 NaN
2 277 315.000000
3 38 256.000000
4 1 105.333333


I was expecting MMeans to start at index 1 since 1 is the mean of (0-1-2). Is there an option that I am missing with rolling method?



edit 1



print(pd.DataFrame({
'Freq':eff,
'MMeans': dF['Freq'].rolling(3).mean()}))


edit 2
Sorry @Yuca for not being as clear as I'd like to. Next is the columns I'd like pandas to return :



    Freq      MMeans
0 215 NaN
1 453 315.000000
2 277 256.000000
3 38 105.333333
4 1 29.666667


which are not the results returned with min_periods=2










share|improve this question

























  • without looking at your syntax it is difficult to know what you did. what value did you use for min_periods?

    – Ken Dekalb
    Nov 20 '18 at 21:10











  • how can you get 29 on the last row? also, by using the same logic as for the star of you df, your last row should be NAN

    – Yuca
    Nov 30 '18 at 16:56













  • The list I gave was a partial one, the complete is much more longer. Here is a link with a little longer view of the list and my code to produce what I was expecting.

    – david
    Nov 30 '18 at 17:42














0












0








0








In the (5 first rows) result below, you can see Freq column and the rolling means (3) column MMeans calculated using pandas:



    Freq      MMeans
0 215 NaN
1 453 NaN
2 277 315.000000
3 38 256.000000
4 1 105.333333


I was expecting MMeans to start at index 1 since 1 is the mean of (0-1-2). Is there an option that I am missing with rolling method?



edit 1



print(pd.DataFrame({
'Freq':eff,
'MMeans': dF['Freq'].rolling(3).mean()}))


edit 2
Sorry @Yuca for not being as clear as I'd like to. Next is the columns I'd like pandas to return :



    Freq      MMeans
0 215 NaN
1 453 315.000000
2 277 256.000000
3 38 105.333333
4 1 29.666667


which are not the results returned with min_periods=2










share|improve this question
















In the (5 first rows) result below, you can see Freq column and the rolling means (3) column MMeans calculated using pandas:



    Freq      MMeans
0 215 NaN
1 453 NaN
2 277 315.000000
3 38 256.000000
4 1 105.333333


I was expecting MMeans to start at index 1 since 1 is the mean of (0-1-2). Is there an option that I am missing with rolling method?



edit 1



print(pd.DataFrame({
'Freq':eff,
'MMeans': dF['Freq'].rolling(3).mean()}))


edit 2
Sorry @Yuca for not being as clear as I'd like to. Next is the columns I'd like pandas to return :



    Freq      MMeans
0 215 NaN
1 453 315.000000
2 277 256.000000
3 38 105.333333
4 1 29.666667


which are not the results returned with min_periods=2







python pandas mean






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 20:01







david

















asked Nov 20 '18 at 21:06









daviddavid

18919




18919













  • without looking at your syntax it is difficult to know what you did. what value did you use for min_periods?

    – Ken Dekalb
    Nov 20 '18 at 21:10











  • how can you get 29 on the last row? also, by using the same logic as for the star of you df, your last row should be NAN

    – Yuca
    Nov 30 '18 at 16:56













  • The list I gave was a partial one, the complete is much more longer. Here is a link with a little longer view of the list and my code to produce what I was expecting.

    – david
    Nov 30 '18 at 17:42



















  • without looking at your syntax it is difficult to know what you did. what value did you use for min_periods?

    – Ken Dekalb
    Nov 20 '18 at 21:10











  • how can you get 29 on the last row? also, by using the same logic as for the star of you df, your last row should be NAN

    – Yuca
    Nov 30 '18 at 16:56













  • The list I gave was a partial one, the complete is much more longer. Here is a link with a little longer view of the list and my code to produce what I was expecting.

    – david
    Nov 30 '18 at 17:42

















without looking at your syntax it is difficult to know what you did. what value did you use for min_periods?

– Ken Dekalb
Nov 20 '18 at 21:10





without looking at your syntax it is difficult to know what you did. what value did you use for min_periods?

– Ken Dekalb
Nov 20 '18 at 21:10













how can you get 29 on the last row? also, by using the same logic as for the star of you df, your last row should be NAN

– Yuca
Nov 30 '18 at 16:56







how can you get 29 on the last row? also, by using the same logic as for the star of you df, your last row should be NAN

– Yuca
Nov 30 '18 at 16:56















The list I gave was a partial one, the complete is much more longer. Here is a link with a little longer view of the list and my code to produce what I was expecting.

– david
Nov 30 '18 at 17:42





The list I gave was a partial one, the complete is much more longer. Here is a link with a little longer view of the list and my code to produce what I was expecting.

– david
Nov 30 '18 at 17:42












1 Answer
1






active

oldest

votes


















3














use min_periods =1



df['rol_mean'] = df['Freq'].rolling(3,min_periods=1).mean()


output:



   Freq      MMeans    rol_mean
0 215 NaN 215.000000
1 453 NaN 334.000000
2 277 315.000000 315.000000
3 38 256.000000 256.000000
4 1 105.333333 105.333333





share|improve this answer
























  • sorry, I accepted your answer but this is not excatly what I expect: MMeans column should begin with NaN then 315.000000 etc. As I said in my question, I'd like numbers to begin at index 1.

    – david
    Nov 21 '18 at 17:36













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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401537%2fshifted-results-in-pandas-rolling-mean%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









3














use min_periods =1



df['rol_mean'] = df['Freq'].rolling(3,min_periods=1).mean()


output:



   Freq      MMeans    rol_mean
0 215 NaN 215.000000
1 453 NaN 334.000000
2 277 315.000000 315.000000
3 38 256.000000 256.000000
4 1 105.333333 105.333333





share|improve this answer
























  • sorry, I accepted your answer but this is not excatly what I expect: MMeans column should begin with NaN then 315.000000 etc. As I said in my question, I'd like numbers to begin at index 1.

    – david
    Nov 21 '18 at 17:36


















3














use min_periods =1



df['rol_mean'] = df['Freq'].rolling(3,min_periods=1).mean()


output:



   Freq      MMeans    rol_mean
0 215 NaN 215.000000
1 453 NaN 334.000000
2 277 315.000000 315.000000
3 38 256.000000 256.000000
4 1 105.333333 105.333333





share|improve this answer
























  • sorry, I accepted your answer but this is not excatly what I expect: MMeans column should begin with NaN then 315.000000 etc. As I said in my question, I'd like numbers to begin at index 1.

    – david
    Nov 21 '18 at 17:36
















3












3








3







use min_periods =1



df['rol_mean'] = df['Freq'].rolling(3,min_periods=1).mean()


output:



   Freq      MMeans    rol_mean
0 215 NaN 215.000000
1 453 NaN 334.000000
2 277 315.000000 315.000000
3 38 256.000000 256.000000
4 1 105.333333 105.333333





share|improve this answer













use min_periods =1



df['rol_mean'] = df['Freq'].rolling(3,min_periods=1).mean()


output:



   Freq      MMeans    rol_mean
0 215 NaN 215.000000
1 453 NaN 334.000000
2 277 315.000000 315.000000
3 38 256.000000 256.000000
4 1 105.333333 105.333333






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 21:08









YucaYuca

2,8612625




2,8612625













  • sorry, I accepted your answer but this is not excatly what I expect: MMeans column should begin with NaN then 315.000000 etc. As I said in my question, I'd like numbers to begin at index 1.

    – david
    Nov 21 '18 at 17:36





















  • sorry, I accepted your answer but this is not excatly what I expect: MMeans column should begin with NaN then 315.000000 etc. As I said in my question, I'd like numbers to begin at index 1.

    – david
    Nov 21 '18 at 17:36



















sorry, I accepted your answer but this is not excatly what I expect: MMeans column should begin with NaN then 315.000000 etc. As I said in my question, I'd like numbers to begin at index 1.

– david
Nov 21 '18 at 17:36







sorry, I accepted your answer but this is not excatly what I expect: MMeans column should begin with NaN then 315.000000 etc. As I said in my question, I'd like numbers to begin at index 1.

– david
Nov 21 '18 at 17:36




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401537%2fshifted-results-in-pandas-rolling-mean%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith