Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Pandas - ดึงวันในสัปดาห์จาก DateTimeIndex ด้วย dayofweek


คำอธิบาย

ใช้ property DateTimeIndex.dayofweek ดึงวันในสัปดาห์ (0=จันทร์, 6=อาทิตย์) ของแต่ละ timestamp

ตัวอย่างโค้ด

import pandas as pd

datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='3D')

print("DateTimeIndex...\n", datetimeindex)
print("DateTimeIndex frequency...\n", datetimeindex.freq)
print("\nGet the day of week..\n", datetimeindex.dayofweek)

ผลลัพธ์

DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:50+11:00', '2021-10-23 02:30:50+11:00',
               '2021-10-26 02:30:50+11:00', '2021-10-29 02:30:50+11:00',
               '2021-11-01 02:30:50+11:00', '2021-11-04 02:30:50+11:00'],
              dtype='datetime64[ns, Australia/Sydney]', freq='3D')

Get the day of week..
Int64Index([2, 5, 1, 4, 0, 3], dtype='int64')

2=พุธ, 5=เสาร์, 1=อังคาร, 4=ศุกร์, 0=จันทร์, 3=พฤหัสบดี