Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

주어진 시계열 데이터를 다시 샘플링하고 최대 월말 빈도를 찾는 프로그램을 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

해결책

이 문제를 해결하기 위해 다음 단계를 따릅니다. -

  • 하나의 열로 데이터 프레임 정의,

d = {'Id': [1,2,3,4,5,6,7,8,9,10]}
df = pd.DataFrame(d)
  • start='01/01/2020', 기간 =10 안에 date_range 함수를 만들고 freq ='W'를 할당합니다. 주어진 시작 날짜부터 다음 주 시작 날짜까지 10개의 날짜를 생성하고 df['time_series']로 저장합니다.

df['time_series'] = pd.date_range('01/01/2020', periods=10, freq='W')
  • resample 방법을 적용하여 최대 월말 빈도 찾기,

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