Pandas DataFrame.rolling() Function
Python PandasDataFrame.rolling() function provides a rolling window for mathematical operations.
pandas.DataFrame.rolling()
Syntax
DataFrame.rolling(
window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None
)
parameter
window |
It is a parameter of type integer, offset or BaseIndexer subclass. It specifies the size of the window. Each window has a fixed size. This parameter specifies the number of observations used to calculate the statistics. |
min_periods |
It is an integer parameter. This parameter specifies the minimum number of observations in a window. The number of observations should have a value, otherwise, the result is a null value. |
center |
It is a Boolean parameter. It specifies to set the label in the center of the window. |
win_type |
It is a string parameter. It specifies the type of window. More information here . |
on |
It is a string parameter. It specifies the column name, not the index, over which the rolling window is to be calculated. |
axis |
It is an integer or string parameter. |
closed |
It is a string parameter. It specifies the interval closure. It has four options: right, left, both or neither. |
Return Value
It returns a window after performing a specific operation.
Example Code: DataFrame.rolling()
Find the rolling sum with a window size of 2 using the method
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)
dataframe1 = dataframe.rolling(2).sum()
print("The Rolling Window After Calculation is: \n")
print(dataframe1)
Output:
The Original Data frame is:
Attendance Obtained Marks
0 60 90
1 100 75
2 80 82
3 78 64
4 95 45
The Rolling Window After Calculation is:
Attendance Obtained Marks
0 NaN NaN
1 160.0 165.0
2 180.0 157.0
3 158.0 146.0
4 173.0 109.0
The function returns the rolling sum over the index axis. Note that for index 0, the function returns 0 due to the size of the rolling window NaN
.
Example Codes: Find the rolling mean with a window size of 3 using DataFrame.rolling() Method
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)
dataframe1 = dataframe.rolling(3).mean()
print("The Rolling Window After Calculation is: \n")
print(dataframe1)
Output:
The Original Data frame is:
Attendance Obtained Marks
0 60 90
1 100 75
2 80 82
3 78 64
4 95 45
The Rolling Window After Calculation is:
Attendance Obtained Marks
0 NaN NaN
1 NaN NaN
2 80.000000 82.333333
3 86.000000 73.666667
4 84.333333 63.666667
This function returns the rolling mean window.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to Convert DataFrame Column to String in Pandas
Publish Date:2025/05/02 Views:161 Category:Python
-
We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a
How to count the frequency of values in a Pandas DataFrame
Publish Date:2025/05/02 Views:84 Category:Python
-
Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().
How to get value from Pandas DataFrame cell
Publish Date:2025/05/02 Views:147 Category:Python
-
We'll look at using to get values from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .
How to Add a Row to a Pandas DataFrame
Publish Date:2025/05/02 Views:127 Category:Python
-
Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to
How to change the order of Panas DataFrame columns
Publish Date:2025/05/02 Views:184 Category:Python
-
We will show how to use insert and reindex to change the order of columns in different ways pandas.DataFrame , such as assigning column names in a desired order. pandas.DataFrame Sort the columns in the new order The easiest way is columns
How to pretty print an entire Pandas Series/DataFrame
Publish Date:2025/05/02 Views:167 Category:Python
-
We will introduce various methods to pretty print the entire Pandas Series/DataFrame, such as option_context, set_option, and options.display. option_context Pretty Printing Pandas DataFrame We can option_context use with one or more option
How to count the number of NaN occurrences in a Pandas Dataframe column
Publish Date:2025/05/02 Views:144 Category:Python
-
We will look at methods for counting the number of NaN occurrences in a column of a Pandas DataFrame. We have a number of options, including isna() the method for one or more columns, by NaN subtracting the total length from the number of o
How to Convert a Pandas Dataframe to a NumPy Array
Publish Date:2025/05/02 Views:151 Category:Python
-
We will introduce to_numpy() the method to pandas.Dataframe convert a to NumPy an array, which is introduced in pandas v0.24.0, replacing the old .values method. We can define it on Index , Series , and DataFrame objects to_numpy . The old
How to add a header row to a Pandas DataFrame
Publish Date:2025/05/02 Views:161 Category:Python
-
We will look at methods for adding a header row to a pandas dataframe, as well as the option to pass in the names directly in the dataframe or by assigning the column names in a list directly to dataframe.columns the method. We will also in