How to introduce LAG time in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to introduce LAG time in Python?

How to introduce LAG time in Python?

This recipe helps you introduce LAG time in Python

0

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

Relevant Projects

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

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.