1 回答

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
你可以這樣做:
import pandas as pd
def convert_to_category(x):
if x in set([i for i in range(1,7)]):
return 'Dawn'
elif x in set([i for i in range(7,13)]):
return 'Morning'
elif x in set([i for i in range(13,19)]):
return 'Afternoon'
else:
return 'Night'
df = pd.DataFrame({'time':['21:25:00','10:36:00','23:17:00']})
df['period'] = pd.to_datetime(df['time']).dt.hour.apply(lambda x: convert_to_category(x))
time period
0 21:25:00 Night
1 10:36:00 Morning
2 23:17:00 Night
添加回答
舉報(bào)