I have a origin & destination airport and the number of flights between two airports on a certain year.
ORIGIN_AIRPORT DESTINATION_AIRPORT Counts
0 ABE ATL 170
1 ABE DTW 154
2 ABE ORD 69
3 ABI DFW 530
4 ABQ ATL 123
... ... ... ...
4293 XNA MSP 63
4294 XNA ORD 490
4295 YAK CDV 67
4296 YAK JNU 67
4297 YUM PHX 377
Is there a way to form an adjacency matrix in python using this data? There should be a 0 if there is no connection (no flights) between airports and 1 if there is a connection.
The matrix should be N x N. It should look something like this:
Adjacency Matrix:
ABE ABI ABQ ATL DTW ORD DFW
ABE 0 0 0 1 1 1 0
ABI 0 0 0 0 0 0 1
ABQ 0 0 0 1 0 0 0
ATL 1 0 1 0 0 0 0
DTW 1 0 0 0 0 0 0
ORD 1 0 0 0 0 0 0
DFW 0 1 0 0 0 0 0
...
CodePudding user response:
You can use pd.crosstab()
:
pd.crosstab(df["ORIGIN_AIRPORT"], df["DESTINATION_AIRPORT"])
This outputs:
DESTINATION_AIRPORT ATL DFW DTW ORD
ORIGIN_AIRPORT
ABE 1 0 1 1
ABI 0 1 0 0
ABQ 1 0 0 0