Reputation: 77
I have a dataframe like the following:
I wrote a function that would breakdown each timestamp and calculate the number of minutes in between Down and Up times. I haven't been able to get this to iterate for every row.
data1 = str(list(data['Adjusted_Down']))
data2 = str(list(data['Adjusted_Up']))
breakdown(data1, data2)
Code for reference:
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)
Upvotes: 1
Views: 77
Reputation: 1117
First load the columns as datetime as @samuel mention above,its much faster to load the file like this.
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
Upvotes: 1
Reputation: 86
Your question isn't very clear, but if you are wondering how to get the time delta, then I would suggest that when reading the spreadsheet, you use the parse_dates parameter.
data = pd.read_excel('E:\Savers\Python\Python3 - Master\lab.xlsx', parse_dates=['Adjusted_Down', 'Adjusted_Up'])
At that point, you can simply subtract the 2 columns and then convert to the desired unit.
Upvotes: 1
Reputation: 384
My assumption is that you wanted to run breakdown() function for each row in the data df
for index, row in data.iterrows():
data1 = str(row['Adjusted_Down'])
data2 = str(row['Adjusted_Up'])
breakdown(data1, data2)
Upvotes: 1