我有一个DataFrame,用于描述笛卡尔2D宇宙中多个“对象”在几个不同“轨迹”上的运动。对于每个“曲目”,我也都有其“目标位置”。示例数据:
objs = ['car', 'bicycle', 'plane']
moves = [f'mov{i}' for i in range(1,11)]
multi = pd.MultiIndex.from_product([objs, moves, range(10)], names=['obj', 'mov', 'time'])
locations = pd.DataFrame(np.random.rand(300,2), columns=['X','Y'], index=multi)
targets = pd.DataFrame(np.random.rand(10,2), columns=['X','Y'], index=moves)
I'm interested in the euclidean-distance between the locations
and the targets
on each timestamp. Something like
distances = pd.Series(np.random.rand(300), index=multi)
Problem is I can't use Subtract
method since both objects need to have the same index, and can't figure out how to get the 2 DataFrames' Indexes to "fit". Anyone has a nice (efficient) way for me to get those distances?
so apparently, unlike the subtract method that needs completely matching index-es for
self
andother
, the sub method can take a level as an argument.So there's a simple one-liner for calculating these euclidean distances:
movements.sub(targets, level=1).pow(2).sum(axis=1).transform(np.sqrt)
IICU: