I have the following dataframe
in pandas:
import pandas as pd
df = pd.DataFrame({
"CityId": {
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4
},
"X": {
"0": 316.83673906150904,
"1": 4377.40597216624,
"2": 3454.15819771172,
"3": 4688.099297634771,
"4": 1010.6969517482901
},
"elevation_meters": {
"0": 1,
"1": 2,
"2": 3,
"3": 4,
"4": 5
},
"Y": {
"0": 2202.34070733524,
"1": 336.602082171235,
"2": 2820.0530112481106,
"3": 2935.89805580997,
"4": 3236.75098902635
}
})
I am trying to create a distance matrix that represents the cost of moving between each of these CityIds
. Using pdist
and squareform
from scipy.spatial.distance
I can do the following:
from scipy.spatial.distance import pdist, squareform
df_m = pd.DataFrame(
squareform(
pdist(
df[['CityId', 'X', 'Y']].iloc[:, 1:],
metric='euclidean')
),
index=df.CityId.unique(),
columns= df.CityId.unique()
)
This gives me a distance matrix between all the CityIds
using pairwise distances calculated from pdist
.
I would like to incorporate elevation_meters
into the this distance matrix. What is an efficient way to do so?