我在数据框“第一响应时间(HH:MM:SS)和“目标”中有两列,值分别为“ 00:08:40”和“ 00:25:00”,同时减去了IM面临的错误

时间:2019-09-18 09:42:12

标签: python-3.x pandas dataframe

我想从目标中减去第一个响应,如果输出小于目标,则必须在数据帧的新列中添加“ INSLA”。我收到一个错误,因为在'int'和'str'的实例之间不支持'<'。如何将它们都更改为整数值?

我尝试过使用代码

tickets ['SLA'] = np.where(tickets ['First Response Time(HH:MM:SS)']

report = pd.read_excel('9999.xlsx',index_col=0)

report['Requester'] = report['Requester'].str.replace(" ","")  # removes 
space from requester columns
report['Requester']= report['Requester'].str.lower() # converts names to 
lowercase
df = pd.DataFrame(report)

tickets['Target'] = np.where(tickets['Priority'] =="High" ,'00:25:00',
                    np.where(tickets['Priority'] == "Medium",'00:30:00',
                     np.where(tickets['Priority'] == "Normal",'00:30:00',
                      np.where(tickets['Priority']== "Low",'00:30:00',
                       np.where(tickets['Priority'] == "Critical", 
'00:10:00',tickets['Priority'])))))

tickets['SLA'] = np.where(tickets['First Response Time (HH:MM:SS)']< 
tickets['Target'],"INSLA","OSLA")

如果第一个响应小于目标或“ OSLA”,则预期输出为“ INSLA”

实际结果是:

回溯(最近通话最近):   

中的文件“ E:/ Python / python workspace / webScraping / report / test2.py”,第76行
tickets['SLA'] = np.where(tickets['First Response Time (HH:MM:SS)']< tickets['Target'],"INSLA","OSLA")

文件“ C:\ Users \ Kittu \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ pandas \ core \ ops.py”,行1735,在包装器中

res_values = na_op(self.values, other.values)

文件“ C:\ Users \ Kittu \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ pandas \ core \ ops.py”,行1625,位于na_op

result = _comp_method_OBJECT_ARRAY(op, x, y)

_comp_method_OBJECT_ARRAY中第1601行的文件“ C:\ Users \ Kittu \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ pandas \ core \ ops.py”

result = libops.vec_compare(x, y, op)

pandas._libs.ops.vec_compare中的文件“ pandas_libs \ ops.pyx”,第164行 TypeError:“ int”和“ str”的实例之间不支持“ <”

1 个答案:

答案 0 :(得分:1)

在比较日期之前,您必须将列转换为日期时间类型:

tickets['First Response Time (HH:MM:SS)'] = pd.to_datetime(tickets['First Response Time (HH:MM:SS)'], format='%H:%M:%S')
tickets['Target'] = pd.to_datetime(tickets['Target'], format='%H:%M:%S')

(这里我假设Target列与First Response Time (HH:MM:SS)的格式相同)