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

This python source code does the following : 1. Creates your own time series data. 2. Implements Lag time function("shift")for filling nan values. 3. Displays the final result.
In [1]:
## How to introduce LAG time in Python 
def Kickstarter_Example_46():
    print(format('How to introduce LAG time in Python','*^82'))
    import warnings

    # Load library
    import pandas as pd

    # Create data frame
    df = pd.DataFrame()

    # Create data
    df['dates'] = pd.date_range('11/11/2016', periods=5, freq='D')
    df['stock_price'] = [1.1,2.2,3.3,4.4,5.5]

    # Lag Time Data By One Row
    df['previous_days_stock_price'] = df['stock_price'].shift(1)

    # Show data frame
    print(); print(df)

    # Lag Time Data By Two Rows
    df['previous_days_stock_price'] = df['stock_price'].shift(2)

    # Show data frame
    print(); print(df)

***********************How to introduce LAG time in Python************************

       dates  stock_price  previous_days_stock_price
0 2016-11-11          1.1                        NaN
1 2016-11-12          2.2                        1.1
2 2016-11-13          3.3                        2.2
3 2016-11-14          4.4                        3.3
4 2016-11-15          5.5                        4.4

       dates  stock_price  previous_days_stock_price
0 2016-11-11          1.1                        NaN
1 2016-11-12          2.2                        NaN
2 2016-11-13          3.3                        1.1
3 2016-11-14          4.4                        2.2
4 2016-11-15          5.5                        3.3

Relevant Projects

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

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.

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.

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

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

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.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.