{"id":1366,"date":"2024-01-10T13:00:00","date_gmt":"2024-01-10T13:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1366"},"modified":"2024-01-12T05:46:36","modified_gmt":"2024-01-12T05:46:36","slug":"moving-average-pandas","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/moving-average-pandas\/","title":{"rendered":"Calculate Moving Average in Pandas (with code)"},"content":{"rendered":"\n<p>Pandas is a powerful Python library that is near and dear to data scientists. Specifically, one of the most commonly used techniques in time series analysis is the moving average, which is a favored tool for smoothing out data and identifying trends. In this article, we will explore the various techniques we can use to calculate Moving Averages in Pandas.<\/p>\n\n\n\n<p>But first, let us briefly understand the concept of moving averages and its various types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the Moving Average?<\/strong><\/h2>\n\n\n\n<p><strong>A moving average calculates the average of data points over a specified period. This helps smooth out the price fluctuations or noise and identify underlying trends in the data.<\/strong><\/p>\n\n\n\n<p>There are three primary types of moving averages:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Simple Moving Average (SMA):<\/strong> It is the unweighted mean of the previous data points.\u00a0For instance, if you have a series of 10 numbers and you want to calculate a rolling 3-point moving average, the first moving average value would be the average of the first three numbers, the second would be the average of numbers two through four, and so on.<\/li>\n\n\n\n<li><strong>Weighted Moving Average (WMA):<\/strong> As the name suggests, it assigns different weights to the data points. The weights depend on the significance of the data point. The most recent data points might be given more weight because they are more relevant.<\/li>\n\n\n\n<li><strong>Exponential Moving Average (EMA):<\/strong> Exponential Moving Average is a type of weighted moving average where more weight is given to the latest data. EMA is more sensitive to recent price changes than the Simple Moving Average.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Calculating the Moving Average in Pandas<\/strong><\/h2>\n\n\n\n<p>Now that we understand the concept of moving average, let us create a sample dataset and try to calculate the moving average of the data. <\/p>\n\n\n\n<p><strong>Pandas provides a convenient way to calculate moving averages using the rolling() function. <\/strong>This function creates a window of a specified size over the DataFrame and applies a specified function to the values in the window. The size of the window determines the number of data points used to calculate the moving average.<\/p>\n\n\n\n<p>Let us see an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Generate a sample dataset\nnp.random.seed(42)  # for reproducibility\ndate_rng = pd.date_range(start='2022-01-01', end='2022-12-31', freq='D')\ndata = np.random.randint(30, 70, size=(len(date_rng)))\ndf = pd.DataFrame(data, columns=['Value'], index=date_rng)\n\n# Calculate the moving average\nwindow_size = 7  # You can adjust the window size as needed\ndf['Moving_Avg'] = df['Value'].rolling(window=window_size).mean()\n\n# Plot the original data and the moving average\nplt.figure(figsize=(10, 6))\nplt.plot(df['Value'], label='Original Data')\nplt.plot(df['Moving_Avg'], label=f'Moving Average ({window_size} days)')\nplt.title('Original Data and Moving Average')\nplt.xlabel('Date')\nplt.ylabel('Value')\nplt.legend()\nplt.show()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/KI5_5LTXK6eBdkCDbiC4Ruo1yNalKXQIeGSDPlvwSoFfcV2lELKn1NRxGRfLi-mLCtRXNqapW38FIYB5xIiZ18t-9bMr5lqOeQoCloeeH4W9tkI1LES9Wsu-DioBZc-BOh_hegeutSaPyvVOKOMf9N8\" alt=\"Original Data and Moving Average\"\/><\/figure>\n<\/div>\n\n\n<p>Now, let us try to find the various types of moving averages and also see an example,<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Calculating Simple Moving Average<\/strong><\/h3>\n\n\n\n<p>The simple moving average is the most basic type of moving average. It provides a smoothed representation of the data by calculating the average of a fixed number of previous data points. To calculate the simple moving average using Pandas, we can use the rolling() function with the mean() method.\u00a0<\/p>\n\n\n\n<p>Here is the code to do it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Generate a sample dataset\nnp.random.seed(42)  # for reproducibility\ndate_rng = pd.date_range(start='2022-01-01', end='2022-12-31', freq='D')\ndata = np.random.randint(30, 70, size=(len(date_rng)))\ndf = pd.DataFrame(data, columns=['Value'], index=date_rng)\n\n# Calculate the simple moving average\nwindow_size = 20  # You can adjust the window size as needed\ndf['SMA'] = df['Value'].rolling(window=window_size).mean()\n\n# Plot the original data and the moving average\nplt.figure(figsize=(10, 6))\nplt.plot(df['Value'], label='Original Data')\nplt.plot(df['SMA'], label=f'Simple Moving Average ({window_size} days)')\nplt.title('Original Data and Simple Moving Average')\nplt.xlabel('Date')\nplt.ylabel('Value')\nplt.legend()\nplt.show()<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/XcwBfz0BR6iigCJTb3DPYZ47PGAfCAAQHbJqoJ44-CUYEpSMWtS_1axzP6bwdj462mVNmTmOzVuUtAfVnc_zaWtBorFYFKiOko14gY9MRT73RAL83HSOx-7OiBcpfcsGAg4JjfvCbtzuj13y6DmKplE\" alt=\"Simple Moving Average\"\/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Calculating Weighted Moving Average<\/strong><\/h3>\n\n\n\n<p>The weighted moving average assigns different weights to different data points based on their importance or relevance. This type of moving average gives more significance to recent data points.<\/p>\n\n\n\n<p>To calculate the weighted moving average using Pandas, we can use the rolling() function along with the <a href=\"https:\/\/favtutor.com\/articles\/pandas-dataframe-apply\/\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/articles\/pandas-dataframe-apply\/\">apply() method<\/a> and a custom function that applies the weighted calculation. Let us see an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Generate a sample dataset\nnp.random.seed(42)\ndate_rng = pd.date_range(start='2022-01-01', end='2022-12-31', freq='D')\ndata = np.random.randint(30, 70, size=(len(date_rng)))\ndf = pd.DataFrame(data, columns=['Value'], index=date_rng)\n\n# Define a custom function to calculate weighted moving average\ndef weighted_average(window):\n    # Define weights for the weighted moving average\n    weights = np.array([0.1, 0.2, 0.3, 0.2, 0.1])  # Adjust weights as needed\n    return np.sum(window * weights) \/ np.sum(weights)\n\n# Calculate the weighted moving average using apply()\ndf['Weighted_Moving_Avg'] = df['Value'].rolling(window=len(weights), center=True).apply(weighted_average, raw=True)\n\n# Plot the original data and the weighted moving average\nplt.figure(figsize=(10, 6))\nplt.plot(df['Value'], label='Original Data')\nplt.plot(df['Weighted_Moving_Avg'], label='Weighted Moving Average')\nplt.title('Original Data and Weighted Moving Average')\nplt.xlabel('Date')\nplt.ylabel('Value')\nplt.legend()\nplt.show()<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/6WsQbspY1WtqYz2oyQ5BnRSEMsrWaoWIMEfnrlVubrr6kAnyVOUqmT2l_vRG54swY2OQVQqQUy1-VcEh-zRlHS2HZPI4hS2wPvLdSrv6DktFuPp2IcZhmOA9Q7pVCWm5gS-zg4ANKLeN7wm5WogFDSg\" alt=\"Weighted Moving Average\"\/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Calculating Exponential Moving Average<\/strong><\/h3>\n\n\n\n<p>The exponential moving average assigns greater weights to more recent data points, making it more responsive to recent changes in the data. To calculate the exponential moving average using Pandas, we can use the ewm() function with the mean() method.\u00a0Here is the code in Python:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Generate a sample dataset\nnp.random.seed(42)\ndate_rng = pd.date_range(start='2022-01-01', end='2022-12-31', freq='D')\ndata = np.random.randint(30, 70, size=(len(date_rng)))\ndf = pd.DataFrame(data, columns=['Value'], index=date_rng)\n\n# Calculate the Exponential Moving Average (EMA)\nspan = 7  # You can adjust the span parameter as needed\ndf['EMA'] = df['Value'].ewm(span=span, adjust=False).mean()\n\n# Plot the original data and the Exponential Moving Average (EMA)\nplt.figure(figsize=(10, 6))\nplt.plot(df['Value'], label='Original Data')\nplt.plot(df['EMA'], label=f'EMA (span={span})')\nplt.title('Original Data and Exponential Moving Average (EMA)')\nplt.xlabel('Date')\nplt.ylabel('Value')\nplt.legend()\nplt.show()<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/7oWmJ-IA3NleNiuSy58O-UWhKil-zlNx6wiCmMaISIrPhNAlM9ePD3E9uR1beOOcyuVpQdCcPa9oqGVVxoXRjiuUuR4Ku9xQnjTRQYPRaut5lkWQUMUihqtRqGT5AwmBjq4V2RAX_AE7z-fuAvzKF8c\" alt=\"Exponential Moving Average\"\/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Calculating the Moving Average of Multiple Columns<\/strong><\/h3>\n\n\n\n<p>We can also calculate the moving average of multiple columns using the Pandas library in Python. If you want to calculate the moving average for multiple columns in a DataFrame, you can apply the rolling window operation to each column individually.\u00a0<\/p>\n\n\n\n<p>Let us see an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Generate a sample dataset with fewer data points and multiple columns\nnp.random.seed(42)\ndate_rng = pd.date_range(start='2022-01-01', end='2022-01-31', freq='D')  # Fewer data points\ndata = np.random.randint(30, 70, size=(len(date_rng), 3))\ndf = pd.DataFrame(data, columns=['Column1', 'Column2', 'Column3'], index=date_rng)\n\n# Calculate and plot the moving averages for each column\nwindow_size = 3  # Adjusted window size for fewer data points\n\n# Iterate over each column\nfor column in df.columns:\n    # Create a new column for the moving average\n    df[f'{column}_MA'] = df[column].rolling(window=window_size).mean()\n\n# Plot original data and moving averages\nplt.figure(figsize=(10, 6))\n\n# Plot original data\ndf[['Column1', 'Column2', 'Column3']].plot(marker='o', linestyle='-', alpha=0.7, label='Original Data')\n\n# Plot moving averages\ndf[['Column1_MA', 'Column2_MA', 'Column3_MA']].plot(linestyle='--', alpha=0.7, label=f'Moving Average (Window={window_size})')\n\nplt.title('Original Data and Moving Averages for Multiple Columns')\nplt.xlabel('Date')\nplt.ylabel('Value')\nplt.legend()\nplt.show()<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/zVPlmarrcc4H0Z84qI_DTYcmv9aPYQwk7lYQ1VIYtbZWLSoWRm5Szp3xaxvFROPQJ4I7bq13e-g_HCkasmW4Ed8Fwx8O76L1QREjO6zdNsMCYQIcNUirmkWWxfc1k9kb9bo6vk8TtKzV_-KjXLE9Z0Q\" alt=\"Moving Averages for multiple columns\" style=\"aspect-ratio:1.1973969631236443;width:552px;height:auto\"\/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/d-4dyng8uZrjocmHJurp2zRAZ64BahK7ZU1pZvLre6wXGcPRqRYCiZveRfu_8wNax6HtlSYp0rzmHLALZaTsnj4SpzcYBokLTSaUXIPpeP9dg-hW1-dmaJfop_Y9VLfkxoewUfZ13k8gtuC8FGbCCbs\" alt=\"Moving Averages for multiple columns\"\/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we have discussed the Moving Average and its calculation using the Pandas library in Python. We covered the simple moving average (SMA), weighted moving average (WMA), and exponential moving average (EMA). Each type of moving average has its characteristics and applications and can provide valuable insights into time series data. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand how to calculate the moving average in Pandas, including simple, weighted and exponential moving averages with examples.<\/p>\n","protected":false},"author":10,"featured_media":1368,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[35],"tags":[37],"class_list":["post-1366","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-pandas"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=1366"}],"version-history":[{"count":3,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1366\/revisions"}],"predecessor-version":[{"id":1383,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1366\/revisions\/1383"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1368"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}