gpt4 book ai didi

Python 遍历电子表格的每一行

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:54 26 4
gpt4 key购买 nike

我有一个如下所示的数据框:

enter image description here

我编写了一个函数来分解每个时间戳并计算停机时间和运行时间之间的分钟数。我无法让它对每一行进行迭代。

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)

最佳答案

我的假设是您想为数据 df 中的每一行运行 breakdown() 函数

for index, row in data.iterrows():
data1 = str(row['Adjusted_Down'])
data2 = str(row['Adjusted_Up'])
breakdown(data1, data2)

关于Python 遍历电子表格的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58527639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com