How to introduce LAG time in Python?

How to introduce LAG time in Python?

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

Relevant Projects

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Creating your own embeddings using Glove and Word2vec
We all at some point in time wished to create our own language as a child! But what if certain words always cooccur with another in a corpus? Thus you can make your own model which will understand which word goes with which one, which words are often coming together etc. This all can be done by building a custom embeddings model which we create in this project

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Multi-Class Text Classification with Deep Learning using BERT
In this project, we will cover in detail the architecture of a transformer used in natural language processing use cases. We will go through the key nlp areas in the pre-transformer stage like bow, word2vec...and then the origin and gradual refinement of transformers. Finally, we will study one of the most popular state of the art transformer models, called BERT and use it for text classification on a large dataset.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.