新聞中心
Pandas:從時(shí)間序列數(shù)據(jù)中處理,分割和提取統(tǒng)計(jì)信息
如果必須使用時(shí)間序列數(shù)據(jù),Pandas 是一個(gè)非常有用的工具。 在 Pandas 的幫助下,可以執(zhí)行以下操作 -

創(chuàng)新互聯(lián)主要從事網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)龍港,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢建站服務(wù):18980820575
- 使用
pd.date_range包創(chuàng)建一系列日期 - 通過(guò)使用
pd.Series包對(duì)帶有日期數(shù)據(jù)進(jìn)行索引 - 使用
ts.resample包執(zhí)行重新采樣 - 改變頻率
示例
以下示例顯示使用Pandas處理和分割時(shí)間序列數(shù)據(jù)。 請(qǐng)注意,這里使用月度北極濤動(dòng)數(shù)據(jù),可以從 monthly.ao.index.b50.current.ascii 下載并可以轉(zhuǎn)換為文本格式。
處理時(shí)間序列數(shù)據(jù)
要處理時(shí)間序列數(shù)據(jù),您必須執(zhí)行以下步驟 -
第1步 ,導(dǎo)入以下軟件包 -
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
接下來(lái),定義一個(gè)函數(shù),它將從輸入文件中讀取數(shù)據(jù),如以下代碼所示 -
def read_data(input_file):
input_data = np.loadtxt(input_file, delimiter = None)
現(xiàn)在,將這些數(shù)據(jù)轉(zhuǎn)換為時(shí)間序列。 為此,請(qǐng)創(chuàng)建時(shí)間序列的日期范圍。 在這個(gè)例子中,我們保留一個(gè)月的數(shù)據(jù)頻率。 文件中是存儲(chǔ)從 1950年1月開(kāi)始的數(shù)據(jù)。
dates = pd.date_range('1950-01', periods = input_data.shape[0], freq = 'M')
在這一步中,在 Pandas Series 的幫助下創(chuàng)建時(shí)間序列數(shù)據(jù),如下所示 -
output = pd.Series(input_data[:, index], index = dates)
return output
if __name__=='__main__':
如下所示,指定輸入文件的路徑 -
input_file = "/Users/admin/AO.txt"
現(xiàn)在,將列轉(zhuǎn)換為時(shí)間序列格式,如下所示 -
timeseries = read_data(input_file)
最后,使用顯示的命令繪制并可視化數(shù)據(jù) -
plt.figure()
timeseries.plot()
plt.show()
觀察如下圖所示 -
切片時(shí)間序列數(shù)據(jù) 切片涉及僅檢索時(shí)間序列數(shù)據(jù)的一部分。 作為示例的一部分,我們僅在1980年到1990年間對(duì)數(shù)據(jù)進(jìn)行分割。請(qǐng)注意以下執(zhí)行此任務(wù)的代碼 -
timeseries['1980':'1990'].plot()
plt.show()
當(dāng)您運(yùn)行切片時(shí)間序列數(shù)據(jù)的代碼時(shí),可以觀察下圖所示的圖形 -
標(biāo)題名稱:創(chuàng)新互聯(lián)AI教程:AI人工智能Pandas
網(wǎng)站URL:http://fisionsoft.com.cn/article/cdjidde.html


咨詢
建站咨詢
