簡體   English   中英

是否可以將24小時制轉換為一天中的四個類別或四個象限?

[英]Is there a way to convert 24hour time format into four categories or four quadrants of the day?

我正在嘗試將數據中的時間戳分為一天的四類。

這意味着我需要將對象數據類型轉換為類別

早上00:00:00至11:59:59

下午12:00:00至15:59:59

晚上16:00:00至19:59:59

晚上20:00:00至23:59:59

我的時間戳數據看起來像

transaction timestamp
08:26:00
08:26:00
08:26:00
08:26:00
12:26:00
12:45:00
16:26:00
16:28:00
20:28:00
20:34:00

我希望上述列的輸出為

time of day
Morning
Morning
Morning
Morning
Afternoon
Afternoon
Evening
Evening
Night
Night

如何清除此類數據並將其轉換為4類?

您可以通過to_timedelta將值轉換為to_timedelta ,然后使用cut

df['transaction timestamp'] = pd.to_timedelta(df['transaction timestamp'])
#if values are python object times convert to strings
#df['transaction timestamp'] = pd.to_timedelta(df['transaction timestamp'].astype(str))

b = pd.to_timedelta(['00:00:00','12:00:00','16:00:00','20:00:00', '24:00:00'])
l = ['Morning','Afternoon','Evening','Night']
df['time of day'] = pd.cut(df['transaction timestamp'], bins=b, labels=l)

print (df)
  transaction timestamp time of day
0              08:26:00     Morning
1              08:26:00     Morning
2              08:26:00     Morning
3              08:26:00     Morning
4              12:26:00   Afternoon
5              12:45:00   Afternoon
6              16:26:00     Evening
7              16:28:00     Evening
8              20:28:00       Night
9              20:34:00       Night

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM