device_df['hour'] = device_df['timestamp'].map(lambda x: time.localtime(x).tm_hour) device_df['is_night'] = device_df['hour'].map(lambda x: 1 if x >= 22 or x < 6 else 0) device_df['is_daytime'] = device_df['hour'].map(lambda x: 1 if x >= 10 or x < 17 else 0) device_df['is_weekend'] = device_df['timestamp'].map(lambda x: 1 if datetime.datetime.utcfromtimestamp(x).weekday() >= 5 else 0)
时间: 2024-04-11 22:32:26 浏览: 129
这段代码对 `device_df` DataFrame 进行了一些时间特征的处理和添加。
首先,通过 `device_df['timestamp'].map(lambda x: time.localtime(x).tm_hour)`,将 `timestamp` 列中的时间戳转换为小时数,并将结果赋值给新的列 `'hour'`。这里使用了 `time.localtime` 函数来获取时间戳对应的本地时间,并提取小时数。
接下来,通过 `device_df['hour'].map(lambda x: 1 if x >= 22 or x < 6 else 0)`,将 `'hour'` 列中的小时数进行判断,如果小时数大于等于 22 或者小于 6,则将对应的 `'is_night'` 列设为 1,否则设为 0。这样可以判断每个时间戳是否处于夜晚时间段。
然后,通过 `device_df['hour'].map(lambda x: 1 if x >= 10 or x < 17 else 0)`,将 `'hour'` 列中的小时数进行判断,如果小时数大于等于 10 或者小于 17,则将对应的 `'is_daytime'` 列设为 1,否则设为 0。这样可以判断每个时间戳是否处于白天时间段。
最后,通过 `device_df['timestamp'].map(lambda x: 1 if datetime.datetime.utcfromtimestamp(x).weekday() >= 5 else 0)`,将 `'timestamp'` 列中的时间戳转换为 UTC 时间,并提取该时间对应的星期几。如果星期几大于等于 5(即周六或周日),则将对应的 `'is_weekend'` 列设为 1,否则设为 0。这样可以判断每个时间戳是否处于周末。
通过添加这些时间特征列,可以在后续的分析和建模中考虑时间因素的影响。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)