我有一个DataFrame,其中包含两列,一列是纬度,另一列是地图上位置的经度。另外,我有起点和终点的坐标。 (它们不在DataFrame中)
我想创建一个在起点和终点之间定向的图形网络(矩阵),如下图所示。
#Desired output
np.array([[0,1,1,0,0,0],
[0,0,1,1,1,0],
[0,0,0,1,1,0],
[0,0,0,0,1,1],
[0,0,0,0,0,1],
[0,0,0,0,0,0]])
到目前为止,我已经尝试过此代码:
#start_df and end_df are DataFrames containing lat and lon of start and end points
#location_inbetween containts lat and lon of points between start and end
graph_df = pd.concat([start_df,location_inbetween,end_df],axis=0)
graph_df.reset_index(inplace = True,drop = True)
a = np.zeros(shape=(graph_df.shape[0],graph_df.shape[0]))
graph_mat = pd.DataFrame(a)
for i in range(graph_df.shape[0]):
for j in range(graph_df.shape[0]):
test = ( abs(graph_df.iloc[i,1]) < abs(graph_df.iloc[j,1]) ) | ( abs(graph_df.iloc[i,0]) < abs(graph_df.iloc[j,0]) )
if test == True:
graph_mat.iloc[i,j] = 1
但这不是一个好答案。
def First(graph_df):
graph_mat = pd.DataFrame(np.zeros(shape=(graph_df.shape[0],graph_df.shape[0])))
for i in range(graph_df.shape[0]):
for j in range(graph_df.shape[0]):
test = ( abs(graph_df.iloc[i,1]) < abs(graph_df.iloc[j,1]) ) | ( abs(graph_df.iloc[i,0]) < abs(graph_df.iloc[j,0]) )
if test == True:
graph_mat.iloc[i,j] = 1
for i in range(graph_mat.shape[0]):
for j in range(graph_mat.shape[0]):
for k in range(graph_mat.shape[0]):
test1 = ( abs(graph_df.iloc[i,1]) < abs(graph_df.iloc[j,1]) ) & ( abs(graph_df.iloc[j,1]) < abs(graph_df.iloc[k,1]) ) & ( abs(graph_df.iloc[i,1]) < abs(graph_df.iloc[k,1]) )
test2 = ( abs(graph_df.iloc[i,0]) < abs(graph_df.iloc[j,0]) ) & ( abs(graph_df.iloc[j,0]) < abs(graph_df.iloc[k,0]) ) & ( abs(graph_df.iloc[i,0]) < abs(graph_df.iloc[k,0]) )
test3 = (graph_mat.iloc[i,j] == 1) & (graph_mat.iloc[j,k] ==1)
test4 = test1 & test2 & test3
if test4 == True:
graph_mat.iloc[i,k] = 0
return graph_mat
graph_mat = First(graph_df)