Python Pandas - 獲取時段所在星期幾
要獲取時段所在星期,請使用 period.dayofweek 屬性
首先,匯入必需的庫 -
import pandas as pd
pandas.Period 表示一段時間。建立兩個 Period 物件 -
period1 = pd.Period("2021-09-18")
period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55)顯示 Period 物件 -
print("Period1...\n", period1)
print("Period2...\n", period2)從兩個 Period 物件中獲取星期幾 -
res1 = period1.dayofweek res2 = period2.dayofweek
示例
以下為程式碼 -
import pandas as pd
# The pandas.Period represents a period of time
# creating two Period objects
period1 = pd.Period("2021-09-18")
period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55)
# display the Period objects
print("Period1...\n", period1)
print("Period2...\n", period2)
# get the day of week from two Period objects
res1 = period1.dayofweek
res2 = period2.dayofweek
# Return the day of the week from the two Period objects
# Results shows Monday=0, Tuesday=1, ... ,Sunday=6
print("\nDay of the week from the 1st Period object ...\n", res1)
print("\nDay of the week from the 2nd Period object...\n", res2)輸出
這將產生以下程式碼 -
Period1... 2021-09-18 Period2... 2021-09-22
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP