TransWikia.com

I have a csv file with time as the datetime format and want to edit the same column into miliseconds, how do I change that?

Data Science Asked by Nehi Patel on April 24, 2021

Input file is :

time    event   earthAcceleration.x earthAcceleration.y earthAcceleration.z             
2017-11-22T09:20:13.944 motion  -0.006379993    -0.001029071 -0.010781396   
2017-11-22T09:20:13.954 motion  0.014739769 0.008146121 -0.001068155                
2017-11-22T09:20:13.964 motion  0.008520941 -0.008222823    0.022573506             
2017-11-22T09:20:13.974 motion  0.000573836 0.016701656 -0.016170829                
2017-11-22T09:20:13.984 motion  0.016282924 0.003181049 0.006969224

I need to remove the date and hours and just need the milliseconds from the start. Example: Time2 = time2-time1 i.e. I need relative time in milliseconds such that I can plot a graph by time.

Output:
(the time column should change into)

time
0.00
0.15
1.12
1.34

One Answer

Familiarize yourself with the pandas library, which allows you to do this:

from pandas import read_csv
from io import StringIO
csv_file = StringIO("""time event earthAcceleration.x earthAcceleration.y earthAcceleration.z
2017-11-22T09:20:13.944 motion -0.006379993 -0.001029071 -0.010781396
2017-11-22T09:20:13.954 motion 0.014739769 0.008146121 -0.001068155
2017-11-22T09:20:13.964 motion 0.008520941 -0.008222823 0.022573506
2017-11-22T09:20:13.974 motion 0.000573836 0.016701656 -0.016170829
2017-11-22T09:20:13.984 motion 0.016282924 0.003181049 0.006969224""")
df = read_csv(csv_file, sep=' ', parse_dates=['time'])
df.assign(delta_t = df.time-df.time.ix[0])

I believe your output is wrong; the difference between each line is exactly 0.01s. Welcome to the site!

Answered by Emre on April 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP