I try to figure out how to fix my problem with python. I am using pandas to create a data frame to save my data from a query. I want to extend this data frame with the column "TimeDiff" as you can see in the below table. The calculation of subtracting the time from the first element of time is working. My problem is how to create a new column with this calculated data with correct values.
Time Temperature Humidity Pulse
2013-12-12 12:12:12 25 33 65
2013-12-12 12:12:15 26 30 67
2013-12-12 12:12:18 27 29 68
2013-12-12 12:12:22 28 32 64
Data frame should look like this:
Time Temperature Humidity Pulse TimeDiff
2013-12-12 12:12:12 25 33 65 0
2013-12-12 12:12:15 26 30 67 3
2013-12-12 12:12:18 27 29 68 6
2013-12-12 12:12:22 28 32 64 10
My code for calculating the time difference and add it to the data frame looks like this:
df = pd.DataFrame( [[ij for ij in i] for i in rows] )
df.rename(columns={0: 'Time', 1: 'Temperature', 2: 'Humidity', 3: 'Pulse', 4:'MicValue'}, inplace=True);
df = df.sort(['Time'], ascending=[1]);
sLength = len(df['Time'])
for i in range(sLength):
diff = df['Time'][df.index[i]] - df['Time'][df.index[0]]
totalSeconds = diff.total_seconds()
# df = pd.DataFrame({'DateDiff': totalSeconds}, index=df.index)
# df['Diff'] = df['Diff'].map(totalSeconds)
Executing this code will get the following result:
TimeDiff
740
740
740
740
So the code does not write the actual data into the data frame. It only writes the last element of the calculation of time in the whole column.