这是我的数据的简化版本。我有一个坐标的数据框和一个空的数据框,应该使用提供的函数来填充每一对的距离。
有什么最快的方法来填充这个数据框?尽可能地避免嵌套for循环(慢!)。我可以使用apply或applymap吗?您可以相应地修改函数或其他部分。谢谢。
有什么最快的方法来填充这个数据框?尽可能地避免嵌套for循环(慢!)。我可以使用apply或applymap吗?您可以相应地修改函数或其他部分。谢谢。
import pandas as pd
def get_distance(point1, point2):
"""Gets the coordinates of two points as two lists, and outputs their distance"""
return (((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2 + (point1[2] - point2[2]) ** 2) ** 0.5)
#Dataframe of coordinates.
df = pd.DataFrame({"No.": [25, 36, 70, 95, 112, 101, 121, 201], "x": [1,2,3,4,2,3,4,5], "y": [2,3,4,5,3,4,5,6], "z": [3,4,5,6,4,5,6,7]})
df.set_index("No.", inplace = True)
#Dataframe to be filled with each pair distance.
df_dist = pd.DataFrame({'target': [112, 101, 121, 201]}, columns=["target", 25, 36, 70, 95])
df_dist.set_index("target", inplace = True)