输入文件是:
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
我需要转换第一列的格式并在同一列中存储新值。我需要删除日期和小时,只需要毫秒的时间。示例:Time2 = time2- time1,即我需要以毫秒为单位的相对时间,以便我可以按时间绘制图形。
所需输出:(时间列应更改为)
time
0.01
0.012
1.02
1.04
我得到的代码是字符串输入但是如何处理csv文件的整列,我无法手动添加这些值。
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""")
df = read_csv(csv_file, sep=' ', parse_dates=['time'])
df.assign(delta_t = df.time-df.time.iloc[0])
答案 0 :(得分:0)
我认为以下是您想要的 - 我已经使代码更加冗长而不是严格要求使步骤清晰:
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""")
df = read_csv(csv_file, sep=' ', parse_dates=['time'])
#Additional code:
df['time'] = pd.to_datetime(df['time'])
start_time = df.time.min()
df['millisecond_delta'] = (df['time'] - start_time)/dt.timedelta(milliseconds=1)