Python 在条件语句中使用时间和数值创建分类列

Python 在条件语句中使用时间和数值创建分类列,python,dataframe,time-series,conditional-statements,categories,Python,Dataframe,Time Series,Conditional Statements,Categories,我尝试执行if语句,使用时间和数值来创建一个新的列分类列 条件-如果时间介于05:00:00和19:00:00之间且t_值>0&t_值y为上午9点IC?它应该是CYes,编辑过的示例工作得很好!要不要再来一杯?在这里,我一直试图删除日期(所有时间块),如果不正确的值计数大于2 t_value 2020-05-17 00:00:00 0 2020-05-17 01:00:00 0 2020-05-17 02:00:00 0 2020-05-17 0

我尝试执行if语句,使用时间和数值来创建一个新的列分类列


条件-如果时间介于05:00:00和19:00:00之间且t_值>0&t_值y为上午9点IC?它应该是CYes,编辑过的示例工作得很好!要不要再来一杯?在这里,我一直试图删除日期(所有时间块),如果不正确的值计数大于2
                   t_value 
2020-05-17 00:00:00 0     
2020-05-17 01:00:00 0
2020-05-17 02:00:00 0
2020-05-17 03:00:00 0
2020-05-17 04:00:00 0
2020-05-17 05:00:00 0
2020-05-17 06:00:00 0
2020-05-17 07:00:00 8
2020-05-17 08:00:00 9
2020-05-17 09:00:00 10
2020-05-17 10:00:00 11
2020-05-17 11:00:00 12 
                t_value  C/IC
2020-05-17 00:00:00 0    NA
2020-05-17 01:00:00 0    NA
2020-05-17 02:00:00 0    NA
2020-05-17 03:00:00 0    NA
2020-05-17 04:00:00 0    NA
2020-05-17 05:00:00 0    IC
2020-05-17 06:00:00 0    IC
2020-05-17 07:00:00 8    C
2020-05-17 08:00:00 9    C
2020-05-17 09:00:00 10   C
2020-05-17 10:00:00 11   C
2020-05-17 11:00:00 12   C
#convert to datetime index
df.index = pd.to_datetime(df.index)

#get condition for time boundary
cond1 = df.between_time( '05:00:00', '19:00:00')

print(cond1.index)
DatetimeIndex(['2020-05-17 05:00:00', '2020-05-17 06:00:00',
               '2020-05-17 07:00:00', '2020-05-17 08:00:00',
               '2020-05-17 09:00:00', '2020-05-17 10:00:00',
               '2020-05-17 11:00:00'],
              dtype='datetime64[ns]', freq=None)

#get index to match the t_value conditions

#indices that match time boundary, but not t_value boundary
ic = cond1.loc[~(cond1.t_value.gt(0)) & (cond1.t_value.le(13))].index

#indices that match time boundary and t_value boundary
c = cond1.loc[(cond1.t_value.gt(0)) & (cond1.t_value.le(13))].index

#assign value
df.loc[c,'C/IC'] = "C"
df.loc[ic,'C/IC'] = "IC"

print(df)

    t_value C/IC
2020-05-17 00:00:00 0   NaN
2020-05-17 01:00:00 0   NaN
2020-05-17 02:00:00 0   NaN
2020-05-17 03:00:00 0   NaN
2020-05-17 04:00:00 0   NaN
2020-05-17 05:00:00 0   IC
2020-05-17 06:00:00 0   IC
2020-05-17 07:00:00 8   C
2020-05-17 08:00:00 9   C
2020-05-17 09:00:00 10  C
2020-05-17 10:00:00 11  C
2020-05-17 11:00:00 12  C