计算加速度给定速度

时间:2017-11-08 07:30:20

标签: python pandas physics data-science

对不起,如果这看起来像个愚蠢的问题, 我有一个看起来像这样的数据集

type    time    latitude    longitude   altitude (m)    speed (km/h)    name    desc    currentdistance timeelapsed
T   2017-10-07 10:44:48 28.750766667    77.088805000    783.5   0.0 2017-10-07_10-44-48     0.0 00:00:00
T   2017-10-07 10:44:58 28.752345000    77.087840000    853.5   7.8         198.70532   00:00:10
T   2017-10-07 10:45:00 28.752501667    77.087705000    854.5   7.7         220.53915   00:00:12

我不确定如何处理这个,计算加速度需要考虑速度和时间的差异,对我可能尝试的任何建议?

提前致谢

1 个答案:

答案 0 :(得分:4)

假设您的数据是从CSV加载的,如下所示:

type,time,latitude,longitude,altitude (m),speed (km/h),name,desc,currentdistance,timeelapsed
T,2017-10-07 10:44:48,28.750766667,77.088805000,783.5,0.0,2017-10-07_10-44-48,,0.0,00:00:00
T,2017-10-07 10:44:58,28.752345000,77.087840000,853.5,7.8,,,198.70532,00:00:10
T,2017-10-07 10:45:00,28.752501667,77.087705000,854.5,7.7,,,220.53915,00:00:12

time列转换为datetime对象,timeelapsed列转换为秒。您可以在此处添加acceleration列 计算每行之间speed (km/h)的差异除以每行之间time的差异,如下所示:

from datetime import datetime    
import pandas as pd
import numpy as np

df = pd.read_csv('input.csv', parse_dates=['time'], dtype={'name':str, 'desc':str})
df['timeelapsed'] = (pd.to_datetime(df['timeelapsed'], format='%H:%M:%S') - datetime(1900, 1, 1)).dt.total_seconds()
df['acceleration'] = (df['speed (km/h)'] - df['speed (km/h)'].shift(1)) / (df['timeelapsed'] - df['timeelapsed'].shift(1))

print df

给你:

  type                time   latitude  longitude  altitude (m)  speed (km/h)                 name desc  currentdistance  timeelapsed  acceleration
0    T 2017-10-07 10:44:48  28.750767  77.088805         783.5           0.0  2017-10-07_10-44-48  NaN          0.00000          0.0           NaN
1    T 2017-10-07 10:44:58  28.752345  77.087840         853.5           7.8                  NaN  NaN        198.70532         10.0          0.78
2    T 2017-10-07 10:45:00  28.752502  77.087705         854.5           7.7                  NaN  NaN        220.53915         12.0         -0.05