Reputation: 33
I am facing a problem with manipulation this table:
date time V_1 I_1 V_2 I_2 Temp
07.07.2017 12:36:27.801 0.000 0.532 0.001 1.289 25.655
07.07.2017 12:36:27.802 0.000 0.486 0.001 1.273 25.655
07.07.2017 12:36:27.803 0.000 0.482 0.001 1.322 25.655
07.07.2017 12:36:27.804 0.000 0.435 0.001 1.311 25.655
I need the time differences (802-801 = 1ms) between each row but I cannot find any solution to handling this without a loop. Is there a more pythonic way?
Upvotes: 1
Views: 71
Reputation: 862641
Use to_datetime
with diff
and then get total_seconds
and multiple by 1000
for ms
:
df['diff'] = pd.to_datetime(df['date'] + ' ' + df['time']).diff().dt.total_seconds() * 1000
print (df)
date time V_1 I_1 V_2 I_2 Temp diff
0 07.07.2017 12:36:27.801 0.0 0.532 0.001 1.289 25.655 NaN
1 07.07.2017 12:36:27.802 0.0 0.486 0.001 1.273 25.655 1.0
2 07.07.2017 12:36:27.803 0.0 0.482 0.001 1.322 25.655 1.0
3 07.07.2017 12:36:27.804 0.0 0.435 0.001 1.311 25.655 1.0
For comparing first value with column use sub
with iat
for select first value of column:
df['diff'] = pd.to_datetime(df['date'] + ' ' + df['time'])
df['diff'] = df['diff'].sub(df['diff'].iat[0]).dt.total_seconds() * 1000
print (df)
date time V_1 I_1 V_2 I_2 Temp diff
0 07.07.2017 12:36:27.801 0.0 0.532 0.001 1.289 25.655 0.0
1 07.07.2017 12:36:27.802 0.0 0.486 0.001 1.273 25.655 1.0
2 07.07.2017 12:36:27.803 0.0 0.482 0.001 1.322 25.655 2.0
3 07.07.2017 12:36:27.804 0.0 0.435 0.001 1.311 25.655 3.0
Upvotes: 1