How to introduce LAG time in Python?

This recipe helps you introduce LAG time in Python

Recipe Objective

Have you ever tried to shift the datetime to create a lag between data and datetime.

So this is the recipe on we can introduce LAG time in Python.

Step 1 - Import the library

import pandas as pd

We have imported pandas which is needed.

Step 2 - Setting up the Data

We have created a dataset by making features and assining values to them. We have used date_range function to create a datetime dataset with frequency as Weekly. df = pd.DataFrame() df["dates"] = pd.date_range("11/11/2016", periods=5, freq="W") df["stock_price"] = [1.1,2.2,3.3,4.4,5.5]

Step 3 - Creating Lag in data

For better understanding we are first creating a lag of 1 unit and then a lag of 2 unit. We cah do this by shift function. df["previous_days_stock_price"] = df["stock_price"].shift(1) print(df) df["previous_days_stock_price"] = df["stock_price"].shift(2) print(df) So the output comes as

       dates  stock_price  previous_days_stock_price
0 2016-11-13          1.1                        NaN
1 2016-11-20          2.2                        1.1
2 2016-11-27          3.3                        2.2
3 2016-12-04          4.4                        3.3
4 2016-12-11          5.5                        4.4

       dates  stock_price  previous_days_stock_price
0 2016-11-13          1.1                        NaN
1 2016-11-20          2.2                        NaN
2 2016-11-27          3.3                        1.1
3 2016-12-04          4.4                        2.2
4 2016-12-11          5.5                        3.3

Download Materials

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python