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

Python Pandas - PeriodIndex 생성 및 요일 가져오기

<시간/>

PeriodIndex를 생성하려면 pandas.PeriodIndex()를 사용하세요. 방법. PeriodIndex.dayofweek를 사용하여 요일 가져오기 재산.

먼저 필요한 라이브러리를 가져옵니다 -

import pandas as pd

PeriodIndex 개체를 만듭니다. PeriodIndex는 일정 기간을 나타내는 서수 값을 보유하는 변경할 수 없는 ndarray입니다. "freq" 매개변수를 사용하여 주파수를 설정했습니다 -

periodIndex = pd.PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20',
'2021-09-15', '2022-03-12', '2023-06-18'], freq="D")

PeriodIndex 개체 표시 -

print("PeriodIndex...\n", periodIndex)

PeriodIndex 개체에서 요일을 표시합니다. 요일은 월요일=0, 화요일=1 ... 일요일=6 −

로 표시됩니다.
print("\nDays of the week from the PeriodIndex...\n", periodIndex.dayofweek)

예시

다음은 코드입니다 -

import pandas as pd

# Create a PeriodIndex object
# PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time
# We have set the frequency using the "freq" parameter
periodIndex = pd.PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20',
'2021-09-15', '2022-03-12', '2023-06-18'], freq="D")

# Display PeriodIndex object
print("PeriodIndex...\n", periodIndex)

# Display PeriodIndex frequency
print("\nPeriodIndex frequency...\n", periodIndex.freq)

# Display day from the PeriodIndex object
print("\nThe number of days from the PeriodIndex...\n", periodIndex.day)

# Display day of the week from the PeriodIndex object
# Days of week are displayed as Monday=0, Tuesday=1 ... Sunday=6
print("\nDays of the week from the PeriodIndex...\n", periodIndex.dayofweek)

출력

이것은 다음 코드를 생성합니다 -

PeriodIndex...
PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20', '2021-09-15', '2022-03-12', '2023-06-18'],
dtype='period[D]')

PeriodIndex frequency...
<Day>

The number of days from the PeriodIndex...
Int64Index([25, 30, 20, 15, 12, 18], dtype='int64')

Days of the week from the PeriodIndex...
Int64Index([2, 2, 4, 2, 5, 6], dtype='int64')