之前曾询问过similar question,但未收到任何回复
我已经在许多论坛中寻找解决方案。其他问题涉及一年,但我的问题不涉及-只是H:M:S
我在网络上抓取了此data,该邮件返回了
时间-36:42 38:34 1:38:32 1:41:18
此处的数据样本:Source data 1和Source data 2
我需要像这样的几分钟36.70 38.57 98.53 101.30
为此,我尝试了此操作:
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)
但是那不起作用,因为36:42的格式不是H:M:S,所以我尝试使用此格式转换36:42
df1.loc[1:,6] = df1[6]+ timedelta(hours=0)
和此
df1['minutes'] = pd.to_datetime(df1[6], format='%H:%M:%S')
但是没有运气。
我可以在提取阶段这样做吗?我必须做500多个行
row_td = soup.find_all('td')
如果没有,转换为数据帧后如何处理
提前感谢
我没有大熊猫的经验,但是您可能会发现这很有用
...
for i in time_list:
parts = i.split(':')
minutes_multiplier = 1/60
math = 0
for part in reversed(parts):
math += (minutes_multiplier * int(part))
minutes_multiplier *= 60
time_mins.append(math)
...
如果输入(时间增量字符串)仅包含小时/分钟/秒(无天等),则可以使用应用于列的自定义函数:
import pandas as pd
df = pd.DataFrame({'Time': ['36:42', '38:34', '1:38:32', '1:41:18']})
def to_minutes(s):
# split string s on ':', reverse so that seconds come first
# multiply the result as type int with elements from tuple (1/60, 1, 60) to get minutes for each value
# return the sum of these multiplications
return sum(int(a)*b for a, b in zip(s.split(':')[::-1], (1/60, 1, 60)))
df['Minutes'] = df['Time'].apply(to_minutes)
# df['Minutes']
# 0 36.700000
# 1 38.566667
# 2 98.533333
# 3 101.300000
# Name: Minutes, dtype: float64
Edit:我花了一段时间才找到它,但这是this question的变体。我的回答是基于this reply。
您走在正确的轨道上。下面对您的代码进行了一些修改,并获取了会议记录。
创建函数
def get_time(i):
ilist = i.split(':')
if(len(ilist)==3):
h, m, s = i.split(':')
else:
m, s = i.split(':')
h = 0
math = (int(h) * 3600 + int(m) * 60 + int(s))/60
return np.round(math, 2)
使用split调用函数
x = "36:42 38:34 1:38:32 1:41:18"
x = x.split(" ")
xmin = [get_time(i) for i in x]
xmin
[输出] >>
[36.7, 38.57, 98.53, 101.3]