我有一个如下数据框:
我编写了一个函数,该函数将分解每个时间戳并计算停机时间和启动时间之间的分钟数。我还没有让每一行都对此进行迭代。
data1 = str(list(data['Adjusted_Down']))
data2 = str(list(data['Adjusted_Up']))
breakdown(data1, data2)
参考代码:
import pandas as pd
data = pd.read_excel('E:\Savers\Python\Python3 - Master\lab.xlsx')
def breakdown(x, y):
string1 = x.split()
variable1 = string1[0]
dateVariable = variable1.split('-')
variable2 = string1[1]
dateVariable2 = variable2.split(':')
hour = int(dateVariable2[0])
minute = int(dateVariable2[1])
seconds = int(dateVariable2[2])
string1B = y.split()
variable1B = string1B[0]
dateVariableB = variable1B.split('-')
variable2B = string1B[1]
dateVariable2B = variable2B.split(':')
hourB = int(dateVariable2B[0])
minuteB = int(dateVariable2B[1])
secondsB = int(dateVariable2B[2])
if hourB > hour:
sumMinutes = (hourB - hour)*60
sumMinutes = sumMinutes + (minuteB - minute)
print(sumMinutes)
elif hourB == hour:
sumMinutes = (minuteB - minute)
print(sumMinutes)
答案 0 :(得分:1)
我的假设是,您想为数据df中的每一行运行细分()函数
for index, row in data.iterrows():
data1 = str(row['Adjusted_Down'])
data2 = str(row['Adjusted_Up'])
breakdown(data1, data2)
答案 1 :(得分:1)
您的问题不是很清楚,但是如果您想知道如何获取时间增量,那么我建议您在阅读电子表格时使用parse_dates参数。
data = pd.read_excel('E:\Savers\Python\Python3 - Master\lab.xlsx', parse_dates=['Adjusted_Down', 'Adjusted_Up'])
此时,您可以简单地减去2列,然后转换为所需的单位。
答案 2 :(得分:1)
首先按照上述@samuel的方式将列作为日期时间加载,这样加载文件的速度要快得多。
data = pd.read_excel('E:\Savers\Python\Python3 - Master\lab.xlsx', parse_dates=['Adjusted_Down', 'Adjusted_Up'])
#Then you can calculate the timedelta as easy as
data['timedelta-minutes'] = data.Adjusted_Up - data.Adjusted_Down
#convert to minutes
data['timedelta-minutes'] = data['timedelta-minutes'].dt.minute