我正在尝试创建一个新的分类列'Stages_So'
,并将其发布到我的原始数据帧中。你知道吗
Event_Code Timestamp
2053 13/08/2016 11:30
1029 10/09/2016 14:00
2053 02/10/2016 13:15
2053 06/11/2016 16:30
2053 19/11/2016 15:00
2053 03/12/2016 17:30
1029 02/01/2017 15:00
1029 05/02/2017 16:00
2053 11/02/2017 15:00
1029 04/03/2017 15:00
2053 01/04/2017 14:00
1029 21/05/2017 14:00
我尝试了以下功能。你知道吗
def label_stage(row):
if row['Timestamp'] > '2016-08-12' and row['Timestamp'] < '2016-11-07':
return 0
if row['Timestamp'] > '2016-11-18' and row['Timestamp'] < '2017-02-06':
return 1
if row['Timestamp'] > '2017-02-10' and row['Timestamp'] < '2017-05-22':
return 2
df['Stages_So'] = df.apply(lambda row: label_stage(row), axis=1)
但它给出了一个错误。
TypeError: ("Cannot compare type 'Timestamp' with type 'str'", 'occurred at index 957')
。你知道吗
您需要首先通过^{} 将列转换为日期时间,然后通过
datetime
进行比较:另一个更快的解决方案:
相关问题 更多 >
编程相关推荐