pandas 语句
1、對于時間格式數據的處理
有些時候time_stamp是object格式,提取相應的日期,小時,星期等:
方法1
from datetime import datetime
user_trad['time_stamp']=user_trad['time_stamp'].apply(lambda x: datetime.strptime(x,'%Y-%m-%d %H:%M'))
user_trad['hour']=user_trad['time_stamp'].dt.hour
user_trad['date']=user_trad['time_stamp'].dt.date
user_trad['weekday']=user_trad['time_stamp'].dt.weekday
user_trad['hour']=user_trad['time_stamp'].dt.hour
user_trad['date']=user_trad['time_stamp'].dt.date
user_trad['weekday']=user_trad['time_stamp'].dt.weekday
shop_hour_user_cnt['hour_cate']=shop_hour_user_cnt['hour'].apply(am_pm_hour)
方法2
#添加小時
join_table['hour']=int(str(join_table['time_stamp'][0])[11:13])
join_table['hour_type']=join_table['hour'].map(am_pm_hour)
#加上每天是星期幾
join_table['date']=(join_table['time_stamp'][0])[0:4]+str(join_table['time_stamp'][0])[5:7]+str(join_table['time_stamp'][0])[8:10]
join_table['weekday']=datetime.strptime(str(join_table['date'][0]),"%Y%m%d").weekday() +1
join_table['hour']=int(str(join_table['time_stamp'][0])[11:13])
join_table['hour_type']=join_table['hour'].map(am_pm_hour)
#加上每天是星期幾
join_table['date']=(join_table['time_stamp'][0])[0:4]+str(join_table['time_stamp'][0])[5:7]+str(join_table['time_stamp'][0])[8:10]
join_table['weekday']=datetime.strptime(str(join_table['date'][0]),"%Y%m%d").weekday() +1
2、
# 按每個小時去重到店客戶
tmp=user_trad.drop_duplicates(['shop_id','user_id','hour'])
總結
- 上一篇: ubuntu中用命令创建数据库
- 下一篇: MybatisPlus #{param}