怎么将python时间段(Timedelta)转化为int或float数值形式

下面是一个实例:

#选取并构建LRFMC模型的特征
airline_selection = airline[["FFP_DATE","LOAD_TIME","FLIGHT_COUNT","LAST_TO_END",
                             "avg_discount","SEG_KM_SUM"]] #筛选多个列要用两个中括号
#构建L特征
L = pd.to_datetime(airline_selection["LOAD_TIME"]) - pd.to_datetime(airline_selection["FFP_DATE"])
#相减之后变成以day为单位的列
print(pd.to_datetime(airline_selection["LOAD_TIME"])[0:3])
print(L[0:3])
#下面两句实现了 225 days,将这个Timedelta转化为按月份计算的可以数值形式(int或float)
print("hhhhhhh:",L.dt.days[0:3]) #和下一条语句的是同样的效果
L = L.astype("str").str.split().str[0]
L = L.astype("int")/30  #天除以30得到月份
发布了94 篇原创文章 · 获赞 20 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/helloworld0906/article/details/103214433