Euclidean Distance Matrix Using Pandas

前端 未结 3 2102
温柔的废话
温柔的废话 2021-02-09 01:48

I have a .csv file that contains city, latitude and longitude data in the below format:

CITY|LATITUDE|LONGITUDE
A|40.745392|-73.978364
B|42.562786|-114.460503
C|         


        
3条回答
  •  甜味超标
    2021-02-09 02:11

    for i in df["CITY"]:
        for j in df["CITY"]:
            row = df[df["CITY"] == j][["LATITUDE", "LONGITUDE"]]
            latitude = row["LATITUDE"].tolist()[0]
            longitude = row["LONGITUDE"].tolist()[0]
            df.loc[df['CITY'] == i, j] = ((df["LATITUDE"] - latitude)**2 + (df["LONGITUDE"] - longitude)**2)**0.5
    
    df = df.drop(["CITY", "LATITUDE", "LONGITUDE"], axis=1)
    

    This works

提交回复
热议问题