Select 按时间字符串
Select string by time
我有一个数据框,我想按一天中的某些时间段对行进行分类。
我添加了一个名为“turno”的新列,该列应该归类为“NoiteAnterior”、“Dia”和“Noite”。我做了一个函数来做到这一点,但总是出现相同的分类,与时间无关。我需要一些帮助才能知道我错在哪里。这是函数:
dataframe
def turno(event):
early_morning = dt.strptime('00:00:00', '%H:%M:%S').time()
morning = dt.strptime('07:00:00', '%H:%M:%S').time()
night = dt.strptime('19:00:00', '%H:%M:%S').time()
for hour in event:
if early_morning < hour < morning:
return 'NoiteAnterior'
elif morning < hour < night:
return 'Dia'
else:
return 'Noite'
df['turno'] = turno(df['hora_inicio_evento'])
df['hora_inicio_evento'] 列:
['hora_inicio_turno']
我假设 df['hora_inicio_evento']
看起来像 '15:37:14'
。然后你应该首先从这个输入中提取小时。如果它已经是日期时间格式,只需在所有地方使用 event
而不是 event_time
.
def turno(event):
early_morning = dt.strptime('00:00:00', '%H:%M:%S').time()
morning = dt.strptime('07:00:00', '%H:%M:%S').time()
night = dt.strptime('19:00:00', '%H:%M:%S').time()
for e in event:
event_time = dt.strptime(e, '%H:%M:%S').time()
if early_morning.hour < event_time.hour <= morning.hour:
return 'NoiteAnterior'
elif morning.hour < event_time.hour <= night.hour:
return 'Dia'
else:
return 'Noite'
请注意,for in
循环不像您想使用的那样工作,它们遍历可迭代对象的每个元素,并将该单个元素放入循环中,在您的例子中表示为 hour
而不是可迭代本身的属性 hour
。
使用:
from datetime import datetime as dt
df = pd.DataFrame({'hora_inicio_evento':['00:10:00', '07:19:00', '19:00:01']})
def turno(event):
early_morning = 0
morning = 7
night = 19
hour = event.hour
if early_morning <= hour and hour < morning:
return 'NoiteAnterior'
elif morning <= hour and hour < night:
return 'Dia'
else:
return 'Noite'
df['turno'] = pd.to_datetime(df['hora_inicio_evento']).apply(turno)
结果:
根据您的评论:
df['turno'] = df['hora_inicio_evento'].apply(turno)
结果:
基于其他评论:
df = pd.read_excel('time_test.xlsx')
df['turno'] = df['inicio_evento'].apply(turno)
结果:
我有一个数据框,我想按一天中的某些时间段对行进行分类。 我添加了一个名为“turno”的新列,该列应该归类为“NoiteAnterior”、“Dia”和“Noite”。我做了一个函数来做到这一点,但总是出现相同的分类,与时间无关。我需要一些帮助才能知道我错在哪里。这是函数:
dataframe
def turno(event):
early_morning = dt.strptime('00:00:00', '%H:%M:%S').time()
morning = dt.strptime('07:00:00', '%H:%M:%S').time()
night = dt.strptime('19:00:00', '%H:%M:%S').time()
for hour in event:
if early_morning < hour < morning:
return 'NoiteAnterior'
elif morning < hour < night:
return 'Dia'
else:
return 'Noite'
df['turno'] = turno(df['hora_inicio_evento'])
df['hora_inicio_evento'] 列: ['hora_inicio_turno']
我假设 df['hora_inicio_evento']
看起来像 '15:37:14'
。然后你应该首先从这个输入中提取小时。如果它已经是日期时间格式,只需在所有地方使用 event
而不是 event_time
.
def turno(event):
early_morning = dt.strptime('00:00:00', '%H:%M:%S').time()
morning = dt.strptime('07:00:00', '%H:%M:%S').time()
night = dt.strptime('19:00:00', '%H:%M:%S').time()
for e in event:
event_time = dt.strptime(e, '%H:%M:%S').time()
if early_morning.hour < event_time.hour <= morning.hour:
return 'NoiteAnterior'
elif morning.hour < event_time.hour <= night.hour:
return 'Dia'
else:
return 'Noite'
请注意,for in
循环不像您想使用的那样工作,它们遍历可迭代对象的每个元素,并将该单个元素放入循环中,在您的例子中表示为 hour
而不是可迭代本身的属性 hour
。
使用:
from datetime import datetime as dt
df = pd.DataFrame({'hora_inicio_evento':['00:10:00', '07:19:00', '19:00:01']})
def turno(event):
early_morning = 0
morning = 7
night = 19
hour = event.hour
if early_morning <= hour and hour < morning:
return 'NoiteAnterior'
elif morning <= hour and hour < night:
return 'Dia'
else:
return 'Noite'
df['turno'] = pd.to_datetime(df['hora_inicio_evento']).apply(turno)
结果:
根据您的评论:
df['turno'] = df['hora_inicio_evento'].apply(turno)
结果:
基于其他评论:
df = pd.read_excel('time_test.xlsx')
df['turno'] = df['inicio_evento'].apply(turno)
结果: