Python uses PyJanitor's shift, lag, and rolling_ The mean function calculates the rolling average, the difference before and after calculation, etc
**Preparation work for environmental construction**
Before using PyJanitor, it is necessary to first install the relevant software packages and import the required class libraries.
1. Install Python: If Python is not installed, you can go to the official Python website( https://www.python.org/downloads/ )Download the latest version of Python and install it.
2. Install PyJanitor: Use the following command to install PyJanitor.
pip install pyjanitor
3. Import required class libraries: Import the required class libraries in a Python script.
python
import pandas as pd
import janitor as jn
**Required class libraries**
-Pandas: A Python library for data processing and analysis, providing data structures for manipulating structured data.
-Janitor: PyJanitor is an auxiliary class library of Pandas, which provides some functions for Data cleansing and preprocessing.
**Data samples**
Here is an example data:
python
import pandas as pd
data = {
'date': pd.date_range(start='2022-01-01', end='2022-01-10'),
'value': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
}
df = pd.DataFrame(data)
**Example of Rolling Average Calculation**
python
#Import the required class libraries
import pandas as pd
import janitor as jn
#Create sample data
data = {
'date': pd.date_range(start='2022-01-01', end='2022-01-10'),
'value': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
}
df = pd.DataFrame(data)
#Using rolling_ Calculate rolling average using the mean function
df = df.rolling_mean(column_name='value', window=3)
print(df)
**Example of difference calculation before and after**
python
#Import the required class libraries
import pandas as pd
import janitor as jn
#Create sample data
data = {
'date': pd.date_range(start='2022-01-01', end='2022-01-10'),
'value': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
}
df = pd.DataFrame(data)
#Calculate the difference before and after using shift and lag functions
df = df.shift(column_name='value', periods=1).lag(column_name='value')
print(df)
**Complete Code Example**
python
#Import the required class libraries
import pandas as pd
import janitor as jn
#Create sample data
data = {
'date': pd.date_range(start='2022-01-01', end='2022-01-10'),
'value': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
}
df = pd.DataFrame(data)
#Using rolling_ Calculate rolling average using the mean function
df = df.rolling_mean(column_name='value', window=3)
#Calculate the difference before and after using shift and lag functions
df = df.shift(column_name='value', periods=1).lag(column_name='value')
print(df)
**Summary**
In Python, you can use the shift, lag, and rolling provided by the PyJanitor library_ The mean function is used to calculate the rolling average and the difference before and after. Firstly, you need to install the PyJanitor library and import the required class libraries. Then, use rolling_ The mean function can calculate the rolling average by specifying column names and window sizes. The shift and lag functions can be used to calculate the difference before and after, by specifying column names and periods. The entire process can be demonstrated through sample code, which includes preparing for environment construction, importing class libraries, creating sample data, calculating rolling average and difference before and after, and outputting results.