我有一个包含CSV数据的Pandas DataFrame,如下所示:
time animal_id x y
0 1 120 205.29 217.76
1 1 100 169.99 128.78
2 1 200 590.33 505.89
3 1 300 645.15 611.94
4 1 350 866.06 851.76
文件的尺寸为216005行和4个属性/列。
我已经根据“ animal_id”对文件进行了分组,之后又添加了一些我必须计算的其他属性(即距离,average_speed,average_acceleration,positive_acceleration,方向)。
此代码为:
# Apply grouping using 'animal_id' attribute-
data_animal_id = data.groupby('animal_id')
# A dictionary object to hold all groups obtained using group by-
data_animal_id_groups = {}
# Get each animal_id's data from grouping performed-
for animal_id in data_animal_id.groups.keys():
data_animal_id_groups[animal_id] = data_animal_id.get_group(animal_id)
# To reset index for each group-
for animal_id in data_animal_id_groups.keys():
data_animal_id_groups[animal_id].reset_index(drop=True, inplace=True)
# Add additional attributes/columns to each groups-
for aid in data_animal_id_groups.keys():
data = [0 for x in range(data_animal_id_groups[aid].shape[0])]
data_animal_id_groups[aid] = data_animal_id_groups[aid].assign(distance=data)
data_animal_id_groups[aid] = data_animal_id_groups[aid].assign(average_speed=data)
data_animal_id_groups[aid] = data_animal_id_groups[aid].assign(average_acceleration=data)
data_animal_id_groups[aid] = data_animal_id_groups[aid].assign(positive_acceleration=data)
data_animal_id_groups[aid] = data_animal_id_groups[aid].assign(direction=data)
我现在正在尝试使用以下逻辑来计算方向:
for aid in data_animal_id_groups.keys():
print("\nComputing Distance & Direction for Animal ID = {0}\n".format(aid))
for i in range(1, data_animal_id_groups[aid].shape[0] - 1):
x1 = data_animal_id_groups[aid].iloc[i, 2]
y1 = data_animal_id_groups[aid].iloc[i, 3]
x2 = data_animal_id_groups[aid].iloc[i + 1, 2]
y2 = data_animal_id_groups[aid].iloc[i + 1, 3]
# Compute distance between 2 points-
distance = math.sqrt(math.pow((x2 - x1), 2) + math.pow((y2 - y1), 2))
# Compute the direction in DEGREES-
direction = math.degrees(math.atan((y2 - y1) / (x2 - x1)))
if math.isnan(direction):
data_animal_id_groups[aid].loc[i, 'Direction'] = 0
# animal_id.loc[i, 'Direction'] = 0
else:
data_animal_id_groups[aid].loc[i, 'Direction'] = direction
但是,这需要很长时间才能计算出来。因此,我尝试使用以下numpy代码:
direction = np.rad2deg(np.arctan((data_animal_id_groups[312]['y'] - data_animal_id_groups[312]['y'].shift(periods = 1, axis = 0)), (data_animal_id_groups[312]['x'] - data_animal_id_groups[312]['x'].shift(periods = 1, axis = 0))))
但是,这给出了错误:
RecursionError:调用a时超出了最大递归深度 Python对象
我该如何解决?
谢谢