我使用以下代码计算了一列:
df_EVENT5_5['age'] = dt.datetime.now().date() - df_EVENT5_5['dt_old']
df_EVENT5_5['age_no_days'] = df_EVENT5_5['age'].dt.total_seconds()/ (24 * 60 * 60)
由于某种原因,输出列包含时间戳。
如何删除时间戳?
我在下面尝试但没有工作:
remove_timestamp_col = ['COL_1', 'COL_2']
for i in remove_timestamp_col:
df_EVENT5_13[i] = df_EVENT5_13[i].age.days()
答案 0 :(得分:0)
我认为数据中可能会有一些带有时间戳的日期,请尝试仅减去日期:
df_EVENT5_5['age'] = dt.date.today() - df_EVENT5_5['dt_old'].apply(dt.datetime.date)
#This gives you the days difference as a number
df_EVENT5_5['age_no_days'] = df_EVENT5_5['age'].dt.days
#If you want it to have 'Days' in the end, you can use concatenation:
df_EVENT5_5['age_with_days'] = df_EVENT5_5['age_no_days'].astype('str') + ' Days'