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;
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
add a comment |
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
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
add a comment |
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
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
python pandas pandas-groupby moving-average
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
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 withpd.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
add a comment |
You can use rolling
on groupby
object directly as:
df['moving'] = df.groupby('object').rolling(10)['value'].mean()
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 thisgroupby.rolling
– Sandeep Kadapa
Nov 16 '18 at 15:20
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%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
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.
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 withpd.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
add a comment |
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.
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 withpd.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
add a comment |
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.
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.
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 withpd.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
add a comment |
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 withpd.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
add a comment |
You can use rolling
on groupby
object directly as:
df['moving'] = df.groupby('object').rolling(10)['value'].mean()
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 thisgroupby.rolling
– Sandeep Kadapa
Nov 16 '18 at 15:20
add a comment |
You can use rolling
on groupby
object directly as:
df['moving'] = df.groupby('object').rolling(10)['value'].mean()
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 thisgroupby.rolling
– Sandeep Kadapa
Nov 16 '18 at 15:20
add a comment |
You can use rolling
on groupby
object directly as:
df['moving'] = df.groupby('object').rolling(10)['value'].mean()
You can use rolling
on groupby
object directly as:
df['moving'] = df.groupby('object').rolling(10)['value'].mean()
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 thisgroupby.rolling
– Sandeep Kadapa
Nov 16 '18 at 15:20
add a comment |
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 thisgroupby.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
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%2f53339021%2fpython-pandas-calculate-moving-average-within-group%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
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