當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Pandas dataframe.resample()用法及代碼示例

Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。

Pandas dataframe.resample()函數主要用於時間序列數據。
時間序列是按時間順序索引(或列出或繪製圖形)的一係列數據點。最常見的是,時間序列是在連續的等間隔時間點上獲取的序列。這是用於頻率轉換和時間序列重采樣的便捷方法。對象必須具有datetime-like索引(DatetimeIndex,PeriodIndex或TimedeltaIndex),或將datetime-like值傳遞給on或level關鍵字。

用法: DataFrame.resample(rule, how=None, axis=0, fill_method=None, closed=None, label=None, convention=’start’, kind=None, loffset=None, limit=None, base=0, on=None, level=None)

參數:
rule:表示目標轉換的偏移量字符串或對象
axis:int,可選,默認0
closed:{'右左'}
label:{'右左'}
convention:僅對於PeriodIndex,控製使用規則的開始還是結束
loffset:調整重新采樣的時間標簽
base:對於平均細分為1天的頻率,請使用匯總間隔的“origin”。例如,對於“ 5分鍾”頻率,基本範圍可以是0到4。默認值為0。
on:對於DataFrame,使用列而不是索引進行重采樣。列必須為datetime-like。
level:對於MultiIndex,用於重采樣的級別(名稱或數字)。級別必須為datetime-like。

重新采樣會根據實際數據生成唯一的采樣分布。我們可以應用各種頻率來重新采樣我們的時間序列數據。這是分析領域中非常重要的技術。
最常用的時間序列頻率是-
W:每周頻率
M:月末頻率
SM:半月結束頻率(15日和月末)
問:四分之一結束頻率

有許多其他類型的時間序列頻率可用。讓我們看看如何將這些時間序列頻率應用於數據並對其重新采樣。

有關在代碼中使用的CSV文件的鏈接,請單擊此處

這是蘋果從(13-11-17)到(13-11-18)持續1年的股價數據

範例1:按月頻率重新采樣數據

# importing pandas as pd 
import pandas as pd 
  
# By default the "date" column was in string format, 
# we need to convert it into date-time format 
  
# parse_dates =["date"], converts the "date"  
# column to date-time format. We know that  
# resampling works with time-series data only 
# so convert "date" column to index 
  
# index_col ="date", makes "date" column, the index of the data frame 
df = pd.read_csv("apple.csv", parse_dates =["date"], index_col ="date") 
  
# Printing the first 10 rows of dataframe 
df[:10]

# Resampling the time series data based on months 
# we apply it on stock close price 
# 'M' indicates month 
monthly_resampled_data = df.close.resample('M').mean() 
  
# the above command will find the mean closing price 
# of each month for a duration of 12 months. 
monthly_resampled_data

輸出:


範例2:按周頻率重新采樣數據

# importing pandas as pd 
import pandas as pd 
  
# We know that resampling works with time-series data 
# only so convert "date" column to index 
# index_col ="date", makes "date" column. 
  
df = pd.read_csv("apple.csv", parse_dates =["date"], index_col ="date") 
  
# Resampling the time series data based on weekly frequency 
# we apply it on stock open price 'W' indicates week 
weekly_resampled_data = df.open.resample('W').mean() 
  
# find the mean opening price of each week  
# for each week over a period of 1 year. 
weekly_resampled_data

輸出:


範例3:按季度頻率重新采樣數據

# importing pandas as pd 
import pandas as pd 
  
# We know that resampling works with time-series 
#  data only so convert our "date" column to index 
# index_col ="date", makes "date" column 
df = pd.read_csv("apple.csv", parse_dates =["date"], index_col ="date") 
  
# Resampling the time series data 
#  based on Quarterly frequency 
# 'Q' indicates quarter 
  
Quarterly_resampled_data = df.open.resample('Q').mean() 
  
# mean opening price of each quarter 
# over a period of 1 year. 
Quarterly_resampled_data

輸出:



相關用法


注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas dataframe.resample()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。