我有下面的array Object
,基本上是以小时、分钟和秒为单位的时间。我想将此对象转换为分钟,但出现错误。错误似乎是由于解包.split
方法结果时字符串长度不同造成的。有什么建议吗
df6['Chip Time']
0 16:42
1 17:34
2 18:13
3 18:32
4 19:12
...
1453 1:35:08
1454 1:43:41
1455 1:45:36
1456 1:45:40
1457 1:48:13
Name: Chip Time, Length: 1458, dtype: object
time_list = df6['Chip Time'].tolist()
# You can use a for loop to convert 'Chip Time' to minutes
time_mins = []
for i in time_list:
h,m,s = i.split(':')
math = (int(h)*3600+int(m)*60+int(s))/60
time_mins.append(math)
print(time_mins)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-52-ac7d4ab91169> in <module>
3 time_mins = []
4 for i in time_list:
----> 5 h,m,s = i.split(':')
6 math = (int(h)*3600+int(m)*60+int(s))/60
7 time_mins.append(math)
ValueError: not enough values to unpack (expected 3, got 2)
请看前几行。让我们说第二排
17:34
。这就是当你分割它时会发生的事情正如您所看到的,只有2个值,因为您只有一个
:
,并且您正试图将其解压为3个变量h,m,s
,这是无法做到的您有几个选项来克服这个问题
17:34
->0:17:34
如果strin的长度为} 和^{} ,则可以将} ,将秒数除以
5
乘以^{0:
添加到5
,然后将connvert列除以^{60
:详细信息:
使用来自this answer的少量输入,还可以获得时间戳的总秒数,如下所示:
请注意,即使时间字符串中只提供了秒(没有分钟或小时),这也可以工作。当然,如果您想要几分钟的时间,您可以包含
/ 60
。您可以将函数映射到df
列:相关问题 更多 >
编程相关推荐