Python Pandas: Calculate moving average within group



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have a dataframe containing time series for 100 objects:



object period value 
1 1 24
1 2 67
...
1 1000 56
2 1 59
2 2 46
...
2 1000 64
3 1 54
...
100 1 451
100 2 153
...
100 1000 21


I want to calculate moving average with window 10 for the value column. I guess I have to do something like



df.groupby('object').apply(lambda ~calculate MA~) 


and then merge this Series to the original dataframe by object? Can't figure out exact commands










share|improve this question






















  • the moving averages would be less rows, which periods should they be assigned to?

    – Walter Tross
    Nov 16 '18 at 13:48











  • It would be ideal to do like this: for period 1, the MA equals just value from period 1. From period 2, MA = (value_1 + value_2) / 2, and so on until 10. After 10, it's a normal moving average

    – Alexandr Kapshuk
    Nov 16 '18 at 13:52











  • I'm trying to use pd.rolling_mean(), but didn't figure it out yet

    – Alexandr Kapshuk
    Nov 16 '18 at 13:52

















1















I have a dataframe containing time series for 100 objects:



object period value 
1 1 24
1 2 67
...
1 1000 56
2 1 59
2 2 46
...
2 1000 64
3 1 54
...
100 1 451
100 2 153
...
100 1000 21


I want to calculate moving average with window 10 for the value column. I guess I have to do something like



df.groupby('object').apply(lambda ~calculate MA~) 


and then merge this Series to the original dataframe by object? Can't figure out exact commands










share|improve this question






















  • the moving averages would be less rows, which periods should they be assigned to?

    – Walter Tross
    Nov 16 '18 at 13:48











  • It would be ideal to do like this: for period 1, the MA equals just value from period 1. From period 2, MA = (value_1 + value_2) / 2, and so on until 10. After 10, it's a normal moving average

    – Alexandr Kapshuk
    Nov 16 '18 at 13:52











  • I'm trying to use pd.rolling_mean(), but didn't figure it out yet

    – Alexandr Kapshuk
    Nov 16 '18 at 13:52













1












1








1








I have a dataframe containing time series for 100 objects:



object period value 
1 1 24
1 2 67
...
1 1000 56
2 1 59
2 2 46
...
2 1000 64
3 1 54
...
100 1 451
100 2 153
...
100 1000 21


I want to calculate moving average with window 10 for the value column. I guess I have to do something like



df.groupby('object').apply(lambda ~calculate MA~) 


and then merge this Series to the original dataframe by object? Can't figure out exact commands










share|improve this question














I have a dataframe containing time series for 100 objects:



object period value 
1 1 24
1 2 67
...
1 1000 56
2 1 59
2 2 46
...
2 1000 64
3 1 54
...
100 1 451
100 2 153
...
100 1000 21


I want to calculate moving average with window 10 for the value column. I guess I have to do something like



df.groupby('object').apply(lambda ~calculate MA~) 


and then merge this Series to the original dataframe by object? Can't figure out exact commands







python pandas pandas-groupby moving-average






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 13:40









Alexandr KapshukAlexandr Kapshuk

304115




304115












  • the moving averages would be less rows, which periods should they be assigned to?

    – Walter Tross
    Nov 16 '18 at 13:48











  • It would be ideal to do like this: for period 1, the MA equals just value from period 1. From period 2, MA = (value_1 + value_2) / 2, and so on until 10. After 10, it's a normal moving average

    – Alexandr Kapshuk
    Nov 16 '18 at 13:52











  • I'm trying to use pd.rolling_mean(), but didn't figure it out yet

    – Alexandr Kapshuk
    Nov 16 '18 at 13:52

















  • the moving averages would be less rows, which periods should they be assigned to?

    – Walter Tross
    Nov 16 '18 at 13:48











  • It would be ideal to do like this: for period 1, the MA equals just value from period 1. From period 2, MA = (value_1 + value_2) / 2, and so on until 10. After 10, it's a normal moving average

    – Alexandr Kapshuk
    Nov 16 '18 at 13:52











  • I'm trying to use pd.rolling_mean(), but didn't figure it out yet

    – Alexandr Kapshuk
    Nov 16 '18 at 13:52
















the moving averages would be less rows, which periods should they be assigned to?

– Walter Tross
Nov 16 '18 at 13:48





the moving averages would be less rows, which periods should they be assigned to?

– Walter Tross
Nov 16 '18 at 13:48













It would be ideal to do like this: for period 1, the MA equals just value from period 1. From period 2, MA = (value_1 + value_2) / 2, and so on until 10. After 10, it's a normal moving average

– Alexandr Kapshuk
Nov 16 '18 at 13:52





It would be ideal to do like this: for period 1, the MA equals just value from period 1. From period 2, MA = (value_1 + value_2) / 2, and so on until 10. After 10, it's a normal moving average

– Alexandr Kapshuk
Nov 16 '18 at 13:52













I'm trying to use pd.rolling_mean(), but didn't figure it out yet

– Alexandr Kapshuk
Nov 16 '18 at 13:52





I'm trying to use pd.rolling_mean(), but didn't figure it out yet

– Alexandr Kapshuk
Nov 16 '18 at 13:52












2 Answers
2






active

oldest

votes


















2














You can use rolling with transform:



df['moving'] = df.groupby('object')['value'].transform(lambda x: x.rolling(10, 1).mean())


The 1 in rolling is for minimum number of periods.






share|improve this answer























  • Oh it's so beautiful! I love Pandas!

    – Alexandr Kapshuk
    Nov 16 '18 at 13:55






  • 1





    This is the recommended way: I was going to answer with pd.rolling_mean and got the deprecation warning. So, thank you for teaching me something today @zipa

    – Charles Landau
    Nov 16 '18 at 13:55











  • You are welcome :)

    – zipa
    Nov 16 '18 at 13:56


















1














You can use rolling on groupby object directly as:



df['moving'] = df.groupby('object').rolling(10)['value'].mean()





share|improve this answer























  • Hi, I'm getting an error: TypeError: incompatible index of inserted column with frame index. No idea what's the problem...

    – Alexandr Kapshuk
    Nov 16 '18 at 15:17











  • Could you please provide a Minimal, Complete, and Verifiable example.

    – Sandeep Kadapa
    Nov 16 '18 at 15:18











  • @AlexandrKapshuk It will work you can check this groupby.rolling

    – Sandeep Kadapa
    Nov 16 '18 at 15:20












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%2f53339021%2fpython-pandas-calculate-moving-average-within-group%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use rolling with transform:



df['moving'] = df.groupby('object')['value'].transform(lambda x: x.rolling(10, 1).mean())


The 1 in rolling is for minimum number of periods.






share|improve this answer























  • Oh it's so beautiful! I love Pandas!

    – Alexandr Kapshuk
    Nov 16 '18 at 13:55






  • 1





    This is the recommended way: I was going to answer with pd.rolling_mean and got the deprecation warning. So, thank you for teaching me something today @zipa

    – Charles Landau
    Nov 16 '18 at 13:55











  • You are welcome :)

    – zipa
    Nov 16 '18 at 13:56















2














You can use rolling with transform:



df['moving'] = df.groupby('object')['value'].transform(lambda x: x.rolling(10, 1).mean())


The 1 in rolling is for minimum number of periods.






share|improve this answer























  • Oh it's so beautiful! I love Pandas!

    – Alexandr Kapshuk
    Nov 16 '18 at 13:55






  • 1





    This is the recommended way: I was going to answer with pd.rolling_mean and got the deprecation warning. So, thank you for teaching me something today @zipa

    – Charles Landau
    Nov 16 '18 at 13:55











  • You are welcome :)

    – zipa
    Nov 16 '18 at 13:56













2












2








2







You can use rolling with transform:



df['moving'] = df.groupby('object')['value'].transform(lambda x: x.rolling(10, 1).mean())


The 1 in rolling is for minimum number of periods.






share|improve this answer













You can use rolling with transform:



df['moving'] = df.groupby('object')['value'].transform(lambda x: x.rolling(10, 1).mean())


The 1 in rolling is for minimum number of periods.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 13:52









zipazipa

16.3k31738




16.3k31738












  • Oh it's so beautiful! I love Pandas!

    – Alexandr Kapshuk
    Nov 16 '18 at 13:55






  • 1





    This is the recommended way: I was going to answer with pd.rolling_mean and got the deprecation warning. So, thank you for teaching me something today @zipa

    – Charles Landau
    Nov 16 '18 at 13:55











  • You are welcome :)

    – zipa
    Nov 16 '18 at 13:56

















  • Oh it's so beautiful! I love Pandas!

    – Alexandr Kapshuk
    Nov 16 '18 at 13:55






  • 1





    This is the recommended way: I was going to answer with pd.rolling_mean and got the deprecation warning. So, thank you for teaching me something today @zipa

    – Charles Landau
    Nov 16 '18 at 13:55











  • You are welcome :)

    – zipa
    Nov 16 '18 at 13:56
















Oh it's so beautiful! I love Pandas!

– Alexandr Kapshuk
Nov 16 '18 at 13:55





Oh it's so beautiful! I love Pandas!

– Alexandr Kapshuk
Nov 16 '18 at 13:55




1




1





This is the recommended way: I was going to answer with pd.rolling_mean and got the deprecation warning. So, thank you for teaching me something today @zipa

– Charles Landau
Nov 16 '18 at 13:55





This is the recommended way: I was going to answer with pd.rolling_mean and got the deprecation warning. So, thank you for teaching me something today @zipa

– Charles Landau
Nov 16 '18 at 13:55













You are welcome :)

– zipa
Nov 16 '18 at 13:56





You are welcome :)

– zipa
Nov 16 '18 at 13:56













1














You can use rolling on groupby object directly as:



df['moving'] = df.groupby('object').rolling(10)['value'].mean()





share|improve this answer























  • Hi, I'm getting an error: TypeError: incompatible index of inserted column with frame index. No idea what's the problem...

    – Alexandr Kapshuk
    Nov 16 '18 at 15:17











  • Could you please provide a Minimal, Complete, and Verifiable example.

    – Sandeep Kadapa
    Nov 16 '18 at 15:18











  • @AlexandrKapshuk It will work you can check this groupby.rolling

    – Sandeep Kadapa
    Nov 16 '18 at 15:20
















1














You can use rolling on groupby object directly as:



df['moving'] = df.groupby('object').rolling(10)['value'].mean()





share|improve this answer























  • Hi, I'm getting an error: TypeError: incompatible index of inserted column with frame index. No idea what's the problem...

    – Alexandr Kapshuk
    Nov 16 '18 at 15:17











  • Could you please provide a Minimal, Complete, and Verifiable example.

    – Sandeep Kadapa
    Nov 16 '18 at 15:18











  • @AlexandrKapshuk It will work you can check this groupby.rolling

    – Sandeep Kadapa
    Nov 16 '18 at 15:20














1












1








1







You can use rolling on groupby object directly as:



df['moving'] = df.groupby('object').rolling(10)['value'].mean()





share|improve this answer













You can use rolling on groupby object directly as:



df['moving'] = df.groupby('object').rolling(10)['value'].mean()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 13:58









Sandeep KadapaSandeep Kadapa

7,408831




7,408831












  • Hi, I'm getting an error: TypeError: incompatible index of inserted column with frame index. No idea what's the problem...

    – Alexandr Kapshuk
    Nov 16 '18 at 15:17











  • Could you please provide a Minimal, Complete, and Verifiable example.

    – Sandeep Kadapa
    Nov 16 '18 at 15:18











  • @AlexandrKapshuk It will work you can check this groupby.rolling

    – Sandeep Kadapa
    Nov 16 '18 at 15:20


















  • Hi, I'm getting an error: TypeError: incompatible index of inserted column with frame index. No idea what's the problem...

    – Alexandr Kapshuk
    Nov 16 '18 at 15:17











  • Could you please provide a Minimal, Complete, and Verifiable example.

    – Sandeep Kadapa
    Nov 16 '18 at 15:18











  • @AlexandrKapshuk It will work you can check this groupby.rolling

    – Sandeep Kadapa
    Nov 16 '18 at 15:20

















Hi, I'm getting an error: TypeError: incompatible index of inserted column with frame index. No idea what's the problem...

– Alexandr Kapshuk
Nov 16 '18 at 15:17





Hi, I'm getting an error: TypeError: incompatible index of inserted column with frame index. No idea what's the problem...

– Alexandr Kapshuk
Nov 16 '18 at 15:17













Could you please provide a Minimal, Complete, and Verifiable example.

– Sandeep Kadapa
Nov 16 '18 at 15:18





Could you please provide a Minimal, Complete, and Verifiable example.

– Sandeep Kadapa
Nov 16 '18 at 15:18













@AlexandrKapshuk It will work you can check this groupby.rolling

– Sandeep Kadapa
Nov 16 '18 at 15:20






@AlexandrKapshuk It will work you can check this groupby.rolling

– Sandeep Kadapa
Nov 16 '18 at 15:20


















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%2f53339021%2fpython-pandas-calculate-moving-average-within-group%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

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党