0
$\begingroup$

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
$\endgroup$

1 Answer 1

1
$\begingroup$

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!

$\endgroup$
10
  • $\begingroup$ I did this code but it does not change my file and doess not get the desired ouput $\endgroup$
    – Nehi Patel
    Nov 29, 2017 at 21:07
  • $\begingroup$ Hey, You wrote each column but I have hundreds of columns how do I do it? $\endgroup$
    – Nehi Patel
    Nov 29, 2017 at 21:20
  • $\begingroup$ What do you mean? I just simulated the csv file by using StringIO; you will skip that step and feed read_csv your actual file. $\endgroup$
    – Emre
    Nov 29, 2017 at 21:24
  • $\begingroup$ from pandas import read_csv from io import StringIO csv_file = open("test.csv', 'a') df = read_csv(csv_file, sep=' ', parse_dates=['time']) df.assign(delta_t = df.time-df.time.iloc[0]) $\endgroup$
    – Nehi Patel
    Nov 29, 2017 at 22:04
  • $\begingroup$ I did this but nothing changed $\endgroup$
    – Nehi Patel
    Nov 29, 2017 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.