Python:使用for循环成对计算加速度
Python: Pairwise computation of acceleration using for loop
这是一个示例数据集,其中包含来自 4 个不同行程的观察结果(有 4 个唯一的行程 ID):
trip_id time_interval speed
0 8a8449635c10cc4b8e7841e517f27e2652c57ea3 873.96 0.062410
1 8a8449635c10cc4b8e7841e517f27e2652c57ea3 11.46 0.000000
2 8a8449635c10cc4b8e7841e517f27e2652c57ea3 903.96 0.247515
3 8a8449635c10cc4b8e7841e517f27e2652c57ea3 882.48 0.121376
4 8a8449635c10cc4b8e7841e517f27e2652c57ea3 918.78 0.185405
5 8a8449635c10cc4b8e7841e517f27e2652c57ea3 885.96 0.122147
6 f7fd70a8c14e43d8be91ef180e297d7195bbe9b0 276.60 0.583178
7 84d14618dcb30c28520cb679e867593c1d29213e 903.48 0.193313
8 84d14618dcb30c28520cb679e867593c1d29213e 899.34 0.085377
9 84d14618dcb30c28520cb679e867593c1d29213e 893.46 0.092259
10 84d14618dcb30c28520cb679e867593c1d29213e 849.36 0.350341
11 3db35f9835db3fe550de194b55b3a90a6c1ecb97 10.86 0.000000
12 3db35f9835db3fe550de194b55b3a90a6c1ecb97 919.50 0.071119
我正在尝试计算从一个点到另一个点的每个独特行程的加速度。
示例:
- 将使用第 0 行和第 1 行(0 初始值;1 最终值)计算第一个加速度值
- 将使用第 1 行和第 2 行(1 个初始值;2 个最终值)计算第二个加速度值
- ...等等。
因为我想根据 trip_id 为每个单独的行程计算这个,这就是我尝试的:
def get_acceleration(dataset):
##### INITIALISATION VARIABLES #####
# Empty string for the trip ID
current_trip_id = ""
# Copy of the dataframe
dataset2 = dataset.copy()
# Creating a new column for the acceleration between observations of the same trip
# all rows have a default value of 0
dataset2["acceleration"] = 0
##### LOOP #####
for index,row in dataset.iterrows():
# Checking if we are looking at the same trip
# when looking at the same trip, the default values of zero are replaced
# by the calculated trip characteristic
if row["trip_id"] == current_trip_id:
# Final speed and time
final_speed = row["speed"]
final_time = row["time_interval"]
print(type(final_speed))
# Computing the acceleration (delta_v/ delta_t)
acceleration = (final_speed[1] - initial_speed[0])/(final_time[1] - initial_time[0])
# Adding the output to the acceleration column
dataset2.loc[index, "acceleration"] = acceleration
##### UPDATING THE LOOP #####
current_trip_id = row["trip_id"]
# Initial speed and time
initial_speed = row["speed"]
initial_time = row["time_interval"]
return dataset2
但是,我收到错误消息:
<ipython-input-42-0255a952850b> in get_acceleration(dataset)
27
28 # Computing the acceleration (delta_v/ delta_t)
---> 29 acceleration = (final_speed[1] - initial_speed[0])/(final_time[1] - initial_time[0])
30
31 # Adding the output to the acceleration column
TypeError: 'float' object is not subscriptable
如何解决这个错误并计算加速度?
更新:
使用下面的答案后,为了避免被零除,只需添加一个 if 和 else 语句。
delta_speed = final_speed - initial_speed
delta_time = final_time - initial_time
# Computing the acceleration (delta_v/ delta_t)
if delta_time != 0:
acceleration = (delta_speed)/(delta_time)
else:
acceleration = 0
有效
acceleration = (final_speed - initial_speed)/(final_time - initial_time)
trip_id
time_interval
speed
acceleration
0
8a8449635c10cc4b8e7841e517f27e2652c57ea3
873.96
0.062410
0.000000
1
8a8449635c10cc4b8e7841e517f27e2652c57ea3
11.46
0.000000
0.000072
2
8a8449635c10cc4b8e7841e517f27e2652c57ea3
903.96
0.247515
0.000277
3
8a8449635c10cc4b8e7841e517f27e2652c57ea3
882.48
0.121376
0.005872
4
8a8449635c10cc4b8e7841e517f27e2652c57ea3
918.78
0.185405
0.001764
5
8a8449635c10cc4b8e7841e517f27e2652c57ea3
885.96
0.122147
0.001927
6
f7fd70a8c14e43d8be91ef180e297d7195bbe9b0
276.60
0.583178
0.000000
7
84d14618dcb30c28520cb679e867593c1d29213e
903.48
0.193313
0.000000
8
84d14618dcb30c28520cb679e867593c1d29213e
899.34
0.085377
0.026071
9
84d14618dcb30c28520cb679e867593c1d29213e
893.46
0.092259
0.001170
10
84d14618dcb30c28520cb679e867593c1d29213e
849.36
0.350341
-0.005852
11
3db35f9835db3fe550de194b55b3a90a6c1ecb97
10.86
0.000000
0.000000
12
3db35f9835db3fe550de194b55b3a90a6c1ecb97
919.50
0.071119
0.000078
这是一个示例数据集,其中包含来自 4 个不同行程的观察结果(有 4 个唯一的行程 ID):
trip_id time_interval speed
0 8a8449635c10cc4b8e7841e517f27e2652c57ea3 873.96 0.062410
1 8a8449635c10cc4b8e7841e517f27e2652c57ea3 11.46 0.000000
2 8a8449635c10cc4b8e7841e517f27e2652c57ea3 903.96 0.247515
3 8a8449635c10cc4b8e7841e517f27e2652c57ea3 882.48 0.121376
4 8a8449635c10cc4b8e7841e517f27e2652c57ea3 918.78 0.185405
5 8a8449635c10cc4b8e7841e517f27e2652c57ea3 885.96 0.122147
6 f7fd70a8c14e43d8be91ef180e297d7195bbe9b0 276.60 0.583178
7 84d14618dcb30c28520cb679e867593c1d29213e 903.48 0.193313
8 84d14618dcb30c28520cb679e867593c1d29213e 899.34 0.085377
9 84d14618dcb30c28520cb679e867593c1d29213e 893.46 0.092259
10 84d14618dcb30c28520cb679e867593c1d29213e 849.36 0.350341
11 3db35f9835db3fe550de194b55b3a90a6c1ecb97 10.86 0.000000
12 3db35f9835db3fe550de194b55b3a90a6c1ecb97 919.50 0.071119
我正在尝试计算从一个点到另一个点的每个独特行程的加速度。
示例:
- 将使用第 0 行和第 1 行(0 初始值;1 最终值)计算第一个加速度值
- 将使用第 1 行和第 2 行(1 个初始值;2 个最终值)计算第二个加速度值
- ...等等。
因为我想根据 trip_id 为每个单独的行程计算这个,这就是我尝试的:
def get_acceleration(dataset):
##### INITIALISATION VARIABLES #####
# Empty string for the trip ID
current_trip_id = ""
# Copy of the dataframe
dataset2 = dataset.copy()
# Creating a new column for the acceleration between observations of the same trip
# all rows have a default value of 0
dataset2["acceleration"] = 0
##### LOOP #####
for index,row in dataset.iterrows():
# Checking if we are looking at the same trip
# when looking at the same trip, the default values of zero are replaced
# by the calculated trip characteristic
if row["trip_id"] == current_trip_id:
# Final speed and time
final_speed = row["speed"]
final_time = row["time_interval"]
print(type(final_speed))
# Computing the acceleration (delta_v/ delta_t)
acceleration = (final_speed[1] - initial_speed[0])/(final_time[1] - initial_time[0])
# Adding the output to the acceleration column
dataset2.loc[index, "acceleration"] = acceleration
##### UPDATING THE LOOP #####
current_trip_id = row["trip_id"]
# Initial speed and time
initial_speed = row["speed"]
initial_time = row["time_interval"]
return dataset2
但是,我收到错误消息:
<ipython-input-42-0255a952850b> in get_acceleration(dataset)
27
28 # Computing the acceleration (delta_v/ delta_t)
---> 29 acceleration = (final_speed[1] - initial_speed[0])/(final_time[1] - initial_time[0])
30
31 # Adding the output to the acceleration column
TypeError: 'float' object is not subscriptable
如何解决这个错误并计算加速度?
更新:
使用下面的答案后,为了避免被零除,只需添加一个 if 和 else 语句。
delta_speed = final_speed - initial_speed
delta_time = final_time - initial_time
# Computing the acceleration (delta_v/ delta_t)
if delta_time != 0:
acceleration = (delta_speed)/(delta_time)
else:
acceleration = 0
有效
acceleration = (final_speed - initial_speed)/(final_time - initial_time)
trip_id | time_interval | speed | acceleration | |
---|---|---|---|---|
0 | 8a8449635c10cc4b8e7841e517f27e2652c57ea3 | 873.96 | 0.062410 | 0.000000 |
1 | 8a8449635c10cc4b8e7841e517f27e2652c57ea3 | 11.46 | 0.000000 | 0.000072 |
2 | 8a8449635c10cc4b8e7841e517f27e2652c57ea3 | 903.96 | 0.247515 | 0.000277 |
3 | 8a8449635c10cc4b8e7841e517f27e2652c57ea3 | 882.48 | 0.121376 | 0.005872 |
4 | 8a8449635c10cc4b8e7841e517f27e2652c57ea3 | 918.78 | 0.185405 | 0.001764 |
5 | 8a8449635c10cc4b8e7841e517f27e2652c57ea3 | 885.96 | 0.122147 | 0.001927 |
6 | f7fd70a8c14e43d8be91ef180e297d7195bbe9b0 | 276.60 | 0.583178 | 0.000000 |
7 | 84d14618dcb30c28520cb679e867593c1d29213e | 903.48 | 0.193313 | 0.000000 |
8 | 84d14618dcb30c28520cb679e867593c1d29213e | 899.34 | 0.085377 | 0.026071 |
9 | 84d14618dcb30c28520cb679e867593c1d29213e | 893.46 | 0.092259 | 0.001170 |
10 | 84d14618dcb30c28520cb679e867593c1d29213e | 849.36 | 0.350341 | -0.005852 |
11 | 3db35f9835db3fe550de194b55b3a90a6c1ecb97 | 10.86 | 0.000000 | 0.000000 |
12 | 3db35f9835db3fe550de194b55b3a90a6c1ecb97 | 919.50 | 0.071119 | 0.000078 |