自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在Pandas中通過(guò)時(shí)間頻率來(lái)匯總數(shù)據(jù)的三種常用方法

開(kāi)發(fā) 前端
當(dāng)我們的數(shù)據(jù)涉及日期和時(shí)間時(shí),分析隨時(shí)間變化變得非常重要。Pandas提供了一種方便的方法,可以按不同的基于時(shí)間的間隔(如分鐘、小時(shí)、天、周、月、季度或年)對(duì)時(shí)間序列數(shù)據(jù)進(jìn)行分組。

當(dāng)我們的數(shù)據(jù)涉及日期和時(shí)間時(shí),分析隨時(shí)間變化變得非常重要。Pandas提供了一種方便的方法,可以按不同的基于時(shí)間的間隔(如分鐘、小時(shí)、天、周、月、季度或年)對(duì)時(shí)間序列數(shù)據(jù)進(jìn)行分組。

圖片

在Pandas中,有幾種基于日期對(duì)數(shù)據(jù)進(jìn)行分組的方法。我們將使用這些虛擬數(shù)據(jù)進(jìn)行演示:

import pandas as pd
import numpy as np
# generating data consisting of weekly sales for the timeperiod Jan,2022 to Jan,2023
dates = pd.date_range('2022-01-01', '2023-01-05', freq = '1 W')
sales_val = np.linspace(1000, 2000,len(dates) )
data = {'date':dates,
'sales': sales_val}
# Load the data
df = pd.DataFrame(data)
# Convert the 'date' column to a datetime type
df['date'] = pd.to_datetime(df['date'])
df.sample(5)

圖片

一些最常用的時(shí)間序列數(shù)據(jù)分組方法是:

1、resample

pandas中的resample 方法用于對(duì)時(shí)間序列數(shù)據(jù)進(jìn)行重采樣,可以將數(shù)據(jù)的頻率更改為不同的間隔。例如將每日數(shù)據(jù)重新采樣為每月數(shù)據(jù)。Pandas中的resample方法可用于基于時(shí)間間隔對(duì)數(shù)據(jù)進(jìn)行分組。它接收f(shuō)requency參數(shù)并返回一個(gè)Resampler對(duì)象,該對(duì)象可用于應(yīng)用各種聚合函數(shù),如mean、sum或count。resample()只在DataFrame的索引為日期或時(shí)間類(lèi)型時(shí)才對(duì)數(shù)據(jù)進(jìn)行重新采樣。

import matplotlib.pyplot as plt
import seaborn as sns
# Set the 'date' column as the index,
# and Group the data by month using resample
grouped = df.set_index('date').resample('M').mean()
print("Grouping is done on monthly basis using resample method:\n", grouped)
# plot the average of monthly sales
sns.lineplot(grouped.index, grouped['sales'])
plt.xlabel("Date")
plt.ylabel("Average Monthly Sales")
plt.grid(True)
plt.title("Average Monthly sales with respect to month")

圖片

在本例中,我們首先將' date '列轉(zhuǎn)換為日期類(lèi)型,然后將其設(shè)置為DataFrame的索引。然后使用重采樣方法按月分組數(shù)據(jù),并計(jì)算每個(gè)月的“sales”列的平均值。結(jié)果是一個(gè)新的DF,每個(gè)月有一行,還包含該月“sales”列的平均值。

2、使用Grouper

pandas的Grouper 函數(shù)可以與 groupby 方法一起使用,以根據(jù)不同的時(shí)間間隔(例如分鐘、小時(shí)、天、周、月、季度或年)對(duì)數(shù)據(jù)進(jìn)行分組。Grouper 包含了key (包含日期的列)、frequency (分組依據(jù)的間隔)、closed (關(guān)閉間隔的一側(cè))和label (標(biāo)記間隔)等參數(shù)。Pandas 中的 Grouper 函數(shù)提供了一種按不同時(shí)間間隔(例如分鐘、小時(shí)、天、周、月、季度或年)對(duì)時(shí)間序列數(shù)據(jù)進(jìn)行分組的便捷方法。通過(guò)與Pandas 中的 groupby 方法 一起使用,可以根據(jù)不同的時(shí)間間隔對(duì)時(shí)間序列數(shù)據(jù)進(jìn)行分組和匯總。

Grouper函數(shù)接受以下參數(shù):

  • key:時(shí)間序列數(shù)據(jù)的列名。
  • freq:時(shí)間間隔的頻率,如“D”表示日,“W”表示周,“M”表示月,等等。
  • closed:間隔是否應(yīng)該在右側(cè)(右)、左側(cè)(左)或兩側(cè)(兩個(gè))閉合。
  • label :用它的結(jié)束(右)或開(kāi)始(左)日期標(biāo)記間隔。
  • Grouper函數(shù)和groupby一起按月間隔對(duì)數(shù)據(jù)進(jìn)行分組:
import matplotlib.pyplot as plt
import seaborn as sns
# Group the data by month using pd.Grouper and calculate monthly average
grouped = df.groupby(pd.Grouper(key='date', freq='M')).mean()
print("Grouping is done on monthly basis using pandas.Grouper and groupby method:\n", grouped)
# plot the average of monthly sales
sns.lineplot(grouped.index, grouped['sales'])
plt.xlabel("Date")
plt.ylabel("Average Monthly Sales")
plt.grid(True)
plt.title("Average Monthly sales with respect to month using pd.Grouper and groupby ")3. Using dt accessor with groupby:

圖片

圖片

3、dt 訪問(wèn)器和 groupby

Pandas中的dt訪問(wèn)器可以從日期和時(shí)間類(lèi)列中提取各種屬性,例如年、月、日等。所以我們可以使用提取的屬性根據(jù)與日期相關(guān)的信息對(duì)數(shù)據(jù)進(jìn)行分組。

在Pandas中,使用dt訪問(wèn)器從DataFrame中的date和time對(duì)象中提取屬性,然后使用groupby方法將數(shù)據(jù)分組為間隔。

import matplotlib.pyplot as plt
import seaborn as sns
# Group the data by month using dt and calculate monthly average
grouped = df.groupby(df['date'].dt.to_period("M")).mean()
print("Grouping is done on monthly basis using dt and groupby method:\n", grouped)

圖片

總結(jié)

這三種常用的方法可以匯總時(shí)間序列數(shù)據(jù),所有方法都相對(duì)容易使用。在時(shí)間復(fù)雜度方面,所有方法對(duì)于中小型數(shù)據(jù)集都是有效的。對(duì)于較大的數(shù)據(jù)集,resample的性能更好,因?yàn)樗槍?duì)時(shí)間索引進(jìn)行了優(yōu)化。而,Grouper和dt提供了更大的靈活性,可以進(jìn)行更復(fù)雜的分組操作??梢愿鶕?jù)自己喜歡的語(yǔ)法或者特定的需求選擇一種方法使用。

責(zé)任編輯:華軒 來(lái)源: DeepHub IMBA
相關(guān)推薦

2023-10-13 10:45:18

HTTP數(shù)據(jù)

2022-05-31 16:00:46

Go 編程語(yǔ)言復(fù)制文件Go 標(biāo)準(zhǔn)庫(kù)

2022-03-22 10:24:48

Linux開(kāi)源Elasticsea

2022-03-04 14:52:27

云計(jì)算開(kāi)源

2023-02-21 14:58:12

間序列周期數(shù)據(jù)集

2010-05-11 14:08:50

MySQL數(shù)字類(lèi)型

2022-07-07 00:33:34

Java線程同步

2009-05-07 15:02:42

OracleJoin查詢(xún)

2018-09-26 10:22:03

2022-11-18 15:09:29

2021-12-20 07:11:26

Java List排序 Java 基礎(chǔ)

2019-07-02 17:34:42

2010-09-10 13:40:09

DIV背景

2010-09-08 13:29:48

CSS

2022-08-19 11:17:09

Linux

2010-07-19 14:43:21

SQL Server查

2010-10-20 13:52:07

SQL Server數(shù)

2023-05-16 16:07:07

大數(shù)據(jù)數(shù)據(jù)管理工具

2021-10-09 06:59:36

技術(shù)MyBatis數(shù)據(jù)

2023-06-29 15:10:48

Web開(kāi)發(fā)CSS
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)