Pandas 係列是帶有軸標簽的一維ndarray。標簽不必是唯一的,但必須是可哈希的類型。該對象同時支持基於整數和基於標簽的索引,並提供了許多用於執行涉及索引的操作的方法。
Pandas Series.rolling()
函數是非常有用的函數。它提供給定Series對象中基礎數據的滾動窗口計算。
用法: Series.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None)
參數:
window:移動窗口的大小
min_periods:具有值的窗口中最小觀察數
center:將標簽設置在窗口的中央。
win_type:提供窗口類型。
on:str,可選
axis:int或str,默認為0
closed:在“右側”,“左側”,“兩個”或“兩個都不”端點上關閉間隔。
返回:用於特定操作的窗口或滾動sub-classed
範例1:采用Series.rolling()
函數查找給定Series對象的基礎數據的滾動窗口總和。滾動窗口的大小應為2,每個元素的權重應相同。
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series([10, 25, 3, 11, 24, 6])
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
# set the index
sr.index = index_
# Print the series
print(sr)
輸出:
現在我們將使用Series.rolling()
函數查找窗口大小為2的基礎數據之和
# Find sum over a window size of 2
result = sr.rolling(2).sum()
# Print the returned Series object
print(result)
輸出:
正如我們在輸出中看到的,Series.rolling()
函數已成功返回一個序列對象,該對象在窗口大小為2的範圍內找到了基礎數據的總和。請注意,第一個值是缺少的值,因為之前沒有元素,因此無法執行總和。
範例2:采用Series.rolling()
函數查找給定Series對象的基礎數據的滾動窗口總和。滾動窗口的大小應為2,滾動窗口類型應為“ triang”。
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series([10, 25, 3, 11, 24, 6])
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
# set the index
sr.index = index_
# Print the series
print(sr)
輸出:
現在我們將使用Series.rolling()
函數查找窗口大小為2的基礎數據之和
# Find sum over a window size of 2
# We have also provided the window type
result = sr.rolling(2, win_type ='triang').sum()
# Print the returned Series object
print(result)
輸出:
正如我們在輸出中看到的,Series.rolling()
函數已成功返回一個序列對象,該對象在窗口大小為2的範圍內找到了基礎數據的總和。請注意,第一個值是缺少的值,因為之前沒有元素,因此無法執行總和。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Timestamp.now用法及代碼示例
- Python Pandas Timestamp.second用法及代碼示例
- Python Pandas DataFrame.abs()用法及代碼示例
- Python Pandas Series.lt()用法及代碼示例
- Python Pandas dataframe.all()用法及代碼示例
- Python Pandas DataFrame.ix[ ]用法及代碼示例
- Python Pandas Series.pop()用法及代碼示例
- Python Pandas TimedeltaIndex.max用法及代碼示例
- Python Pandas Timestamp.dst用法及代碼示例
- Python Pandas Timestamp.tz用法及代碼示例
- Python Pandas Series.mean()用法及代碼示例
- Python Pandas TimedeltaIndex.min用法及代碼示例
- Python Pandas Series.ptp()用法及代碼示例
- Python Pandas dataframe.cov()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas Series.rolling()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。