如何使用时间戳确定是白天还是黑夜 [英] How to find out whether it is Day or Night using Timestamp

查看:324
本文介绍了如何使用时间戳确定是白天还是黑夜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据框中的时间戳列中查找白天还是晚上。时间戳列的值如下:
20:0,14:30,6:15,5:0,4:0等

I want to find out whether it is day or night from the "timestamp" column in my data frame. The time stamp columns have values as follows: 20:0 , 14:30, 6:15, 5:0, 4:0 etc.

I使用了for循环,但它是日夜随机产生的。

I used a for loop but it randomly generated day and night.

for x in data['timestamp']:
if x> '12:00':
     print('Day')

 else:
     print('Night')

我想在我的时间戳列旁边添加一列,当时间戳在6:00 am到18:00之间时,列的值为 Day;当时间戳为0时,列的值为 Night在18:01到5:59上午之间。

I want a column beside my timestamp column which has values as "Day" whenever the timestamp is between 6:00am to 18:00 and "Night" when the timestamp is between 18:01 to 5:59 am.

推荐答案

timestamp = ['6:00', '18:00', '18:01', '5:59']
for time in timestamp:
   hourMin = time.split(":")
   hour = int(hourMin[0])
   mint = int(hourMin[1])
   if hour>= 6 and hour <= 18:
       if(hour == 18):
           if(mint > 0):
               print("Night\n")
           else:
               print("Day\n")
       else:
           print("Day\n")
   else:
       print("Night\n")

这篇关于如何使用时间戳确定是白天还是黑夜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆