用Python编写程序对给定的时间序列数据进行重采样,并找到最大月底频率
假设你拥有时间序列数据以及最大月底频率的结果,
DataFrame is: Id time_series 0 1 2020-01-05 1 2 2020-01-12 2 3 2020-01-19 3 4 2020-01-26 4 5 2020-02-02 5 6 2020-02-09 6 7 2020-02-16 7 8 2020-02-23 8 9 2020-03-01 9 10 2020-03-08 Maximum month end frequency: Id time_series time_series 2020-01-31 4 2020-01-26 2020-02-29 8 2020-02-23 2020-03-31 10 2020-03-08
解决方案
为了解决这个问题,我们将遵循以下步骤:
定义一个只有一列的DataFrame,
d = {'Id': [1,2,3,4,5,6,7,8,9,10]}
df = pd.DataFrame(d)在其中创建`date_range`函数,`start='01/01/2020'`,`periods=10`,并赋值`freq='W'`。它将从给定的起始日期生成十个日期到下一个每周的起始日期,并将其存储为`df['time_series']`。
df['time_series'] = pd.date_range('01/01/2020', periods=10, freq='W')应用重采样方法来查找最大月底频率,
df.resample('M', on='time_series').max())示例
让我们看看下面的实现来更好地理解:
import pandas as pd
d = {'Id': [1,2,3,4,5,6,7,8,9,10]}
df = pd.DataFrame(d)
df['time_series'] = pd.date_range('01/01/2020',
periods=10,
freq='W')
print("DataFrame is:\n",df)
print("Maximum month end frequency: ")
print(df.resample('M', on='time_series').max())输出
DataFrame is: Id time_series 0 1 2020-01-05 1 2 2020-01-12 2 3 2020-01-19 3 4 2020-01-26 4 5 2020-02-02 5 6 2020-02-09 6 7 2020-02-16 7 8 2020-02-23 8 9 2020-03-01 9 10 2020-03-08 Maximum month end frequency: Id time_series time_series 2020-01-31 4 2020-01-26 2020-02-29 8 2020-02-23 2020-03-31 10 2020-03-08
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP