In [35]:
import pandas as pd, json, numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

In [36]:
from pygeocoder import Geocoder
apik='AIzaSyDybC2OroTE_XDJTuxjKruxFpby5VDhEGk'

In [37]:
locations=json.loads(file('locations_ro.json','r').read())

In [38]:
mdf_dest=pd.read_json(json.loads(file('mdf_ro_dest.json','r').read()))
mdf_arrv=pd.read_json(json.loads(file('mdf_ro_arrv.json','r').read()))

In [39]:
citysave_dest=json.loads(file('citysave_ro_dest.json','r').read())
citysave_arrv=json.loads(file('citysave_ro_arrv.json','r').read())

In [40]:
mdf_dest['ID']=mdf_dest['From']
mdf_dest.head()


Out[40]:
Airline Airport City Date From To index ID
0 Blue Air BGY Milan https://www.airportia.com/romania/bacău-airpor... BCM Milan BGY 0 BCM
1 Blue Air DUB Dublin https://www.airportia.com/romania/bacău-airpor... BCM Dublin DUB 1 BCM
10 Blue Air TRN Turin https://www.airportia.com/romania/bacău-airpor... BCM Turin TRN 1 BCM
100 Wizz Air OTP Bucharest https://www.airportia.com/romania/cluj_napoca-... CLJ Bucharest OTP 25 CLJ
1000 Tarom CLJ Cluj-Napoca https://www.airportia.com/romania/henri-coandă... OTP Cluj-Napoca CLJ 110 OTP

In [41]:
mdf_arrv['ID']=mdf_arrv['To']
mdf_arrv.head()


Out[41]:
Airline Airport City Date From To index ID
0 Blue Air BGY Milan https://www.airportia.com/romania/bacău-airpor... Milan BGY BCM 0 BCM
1 Blue Air DUB Dublin https://www.airportia.com/romania/bacău-airpor... Dublin DUB BCM 1 BCM
10 Blue Air LTN London https://www.airportia.com/romania/bacău-airpor... London LTN BCM 1 BCM
100 Wizz Air BCN Barcelona https://www.airportia.com/romania/cluj_napoca-... Barcelona BCN CLJ 28 CLJ
1000 Tarom CLJ Cluj-Napoca https://www.airportia.com/romania/henri-coandă... Cluj-Napoca CLJ OTP 108 OTP

In [42]:
mdf=pd.concat([mdf_dest,mdf_arrv])

In [43]:
mdf


Out[43]:
Airline Airport City Date From To index ID
0 Blue Air BGY Milan https://www.airportia.com/romania/bacău-airpor... BCM Milan BGY 0 BCM
1 Blue Air DUB Dublin https://www.airportia.com/romania/bacău-airpor... BCM Dublin DUB 1 BCM
10 Blue Air TRN Turin https://www.airportia.com/romania/bacău-airpor... BCM Turin TRN 1 BCM
100 Wizz Air OTP Bucharest https://www.airportia.com/romania/cluj_napoca-... CLJ Bucharest OTP 25 CLJ
1000 Tarom CLJ Cluj-Napoca https://www.airportia.com/romania/henri-coandă... OTP Cluj-Napoca CLJ 110 OTP
1001 Turkish Airlines IST Istanbul https://www.airportia.com/romania/henri-coandă... OTP Istanbul IST 111 OTP
1002 Tarom KIV Chisinau https://www.airportia.com/romania/henri-coandă... OTP Chisinau KIV 112 OTP
1003 Qatar Airways DOH Doha https://www.airportia.com/romania/henri-coandă... OTP Doha DOH 113 OTP
1004 El Al TLV Tel Aviv https://www.airportia.com/romania/henri-coandă... OTP Tel Aviv TLV 114 OTP
1005 Tarom BEY Beirut https://www.airportia.com/romania/henri-coandă... OTP Beirut BEY 115 OTP
1006 AirSERBIA BEG Belgrade https://www.airportia.com/romania/henri-coandă... OTP Belgrade BEG 0 OTP
1007 KLM AMS Amsterdam https://www.airportia.com/romania/henri-coandă... OTP Amsterdam AMS 1 OTP
1008 LOT WAW Warsaw https://www.airportia.com/romania/henri-coandă... OTP Warsaw WAW 2 OTP
1009 Wizz Air CIA Rome https://www.airportia.com/romania/henri-coandă... OTP Rome CIA 3 OTP
101 Air Dolomiti MUC Munich https://www.airportia.com/romania/cluj_napoca-... CLJ Munich MUC 0 CLJ
1010 Wizz Air CRL Brussels https://www.airportia.com/romania/henri-coandă... OTP Brussels CRL 4 OTP
1011 Lufthansa MUC Munich https://www.airportia.com/romania/henri-coandă... OTP Munich MUC 5 OTP
1012 Turkish Airlines IST Istanbul https://www.airportia.com/romania/henri-coandă... OTP Istanbul IST 6 OTP
1013 Wizz Air TLV Tel Aviv https://www.airportia.com/romania/henri-coandă... OTP Tel Aviv TLV 7 OTP
1014 Air Berlin TXL Berlin https://www.airportia.com/romania/henri-coandă... OTP Berlin TXL 8 OTP
1015 Wizz Air LTN London https://www.airportia.com/romania/henri-coandă... OTP London LTN 9 OTP
1016 Wizz Air DTM Dortmund https://www.airportia.com/romania/henri-coandă... OTP Dortmund DTM 10 OTP
1017 Blue Air FCO Rome https://www.airportia.com/romania/henri-coandă... OTP Rome FCO 11 OTP
1018 Ryanair CRL Brussels https://www.airportia.com/romania/henri-coandă... OTP Brussels CRL 12 OTP
1019 Ryanair STN London https://www.airportia.com/romania/henri-coandă... OTP London STN 13 OTP
102 Wizz Air LTN London https://www.airportia.com/romania/cluj_napoca-... CLJ London LTN 1 CLJ
1020 Air France CDG Paris https://www.airportia.com/romania/henri-coandă... OTP Paris CDG 14 OTP
1021 Blue Air VLC Valencia https://www.airportia.com/romania/henri-coandă... OTP Valencia VLC 15 OTP
1022 Tarom TSR Timisoara https://www.airportia.com/romania/henri-coandă... OTP Timisoara TSR 16 OTP
1023 Blue Air GLA Glasgow https://www.airportia.com/romania/henri-coandă... OTP Glasgow GLA 17 OTP
... ... ... ... ... ... ... ... ...
972 Blue Air LCA Larnaca https://www.airportia.com/romania/henri-coandă... Larnaca LCA OTP 80 OTP
973 Ryanair MXP Milan https://www.airportia.com/romania/henri-coandă... Milan MXP OTP 81 OTP
974 Blue Air BVA Paris https://www.airportia.com/romania/henri-coandă... Paris BVA OTP 82 OTP
975 Tarom CDG Paris https://www.airportia.com/romania/henri-coandă... Paris CDG OTP 83 OTP
976 Dniproavia KBP Kiev https://www.airportia.com/romania/henri-coandă... Kiev KBP OTP 84 OTP
977 Blue Air LTN London https://www.airportia.com/romania/henri-coandă... London LTN OTP 85 OTP
978 Blue Air OMR Oradea https://www.airportia.com/romania/henri-coandă... Oradea OMR OTP 86 OTP
979 Tarom MAD Madrid https://www.airportia.com/romania/henri-coandă... Madrid MAD OTP 87 OTP
98 Tarom OTP Bucharest https://www.airportia.com/romania/cluj_napoca-... Bucharest OTP CLJ 26 CLJ
980 LOT WAW Warsaw https://www.airportia.com/romania/henri-coandă... Warsaw WAW OTP 88 OTP
981 Ryanair ATH Athens https://www.airportia.com/romania/henri-coandă... Athens ATH OTP 89 OTP
982 Tarom CLJ Cluj-Napoca https://www.airportia.com/romania/henri-coandă... Cluj-Napoca CLJ OTP 90 OTP
983 Austrian Airlines VIE Vienna https://www.airportia.com/romania/henri-coandă... Vienna VIE OTP 91 OTP
984 Wizz Air BUD Budapest https://www.airportia.com/romania/henri-coandă... Budapest BUD OTP 92 OTP
985 Lufthansa MUC Munich https://www.airportia.com/romania/henri-coandă... Munich MUC OTP 93 OTP
986 Qatar Airways DOH Doha https://www.airportia.com/romania/henri-coandă... Doha DOH OTP 94 OTP
987 Wizz Air BGY Milan https://www.airportia.com/romania/henri-coandă... Milan BGY OTP 95 OTP
988 Blue Air LIN Milan https://www.airportia.com/romania/henri-coandă... Milan LIN OTP 96 OTP
989 Meridiana LIN Milan https://www.airportia.com/romania/henri-coandă... Milan LIN OTP 97 OTP
99 Wizz Air BGY Milan https://www.airportia.com/romania/cluj_napoca-... Milan BGY CLJ 27 CLJ
990 Wizz Air CTA Catania https://www.airportia.com/romania/henri-coandă... Catania CTA OTP 98 OTP
991 Wizz Air VRN Verona https://www.airportia.com/romania/henri-coandă... Verona VRN OTP 99 OTP
992 Blue Air STR Stuttgart https://www.airportia.com/romania/henri-coandă... Stuttgart STR OTP 100 OTP
993 Blue Air CLJ Cluj-Napoca https://www.airportia.com/romania/henri-coandă... Cluj-Napoca CLJ OTP 101 OTP
994 Ryanair SXF Berlin https://www.airportia.com/romania/henri-coandă... Berlin SXF OTP 102 OTP
995 Tarom SUJ Satu Mare https://www.airportia.com/romania/henri-coandă... Satu Mare SUJ OTP 103 OTP
996 Tarom IAS Iasi https://www.airportia.com/romania/henri-coandă... Iasi IAS OTP 104 OTP
997 Tarom KIV Chisinau https://www.airportia.com/romania/henri-coandă... Chisinau KIV OTP 105 OTP
998 Tarom LHR London https://www.airportia.com/romania/henri-coandă... London LHR OTP 106 OTP
999 Tarom OMR Oradea https://www.airportia.com/romania/henri-coandă... Oradea OMR OTP 107 OTP

6075 rows × 8 columns


In [44]:
mdg=mdf.set_index(['ID','City','Airport','Airline'])

In [45]:
len(mdg)


Out[45]:
6075

mdg check out with source


In [46]:
flights={}
minn=1.0
for i in mdg.index.get_level_values(0).unique():
    #2 weeks downloaded. want to get weekly freq. but multi by 2 dept+arrv
    d=4.0
    if i not in flights:flights[i]={}
    for j in mdg.loc[i].index.get_level_values(0).unique():
        if len(mdg.loc[i].loc[j])>minn: #minimum 1 flights required in this period once every 2 weeks
            if j not in flights[i]:flights[i][j]={'airports':{},'7freq':0}
            flights[i][j]['7freq']=len(mdg.loc[i].loc[j])/d 
            for k in mdg.loc[i].loc[j].index.get_level_values(0).unique():
                if len(mdg.loc[i].loc[j].loc[k])>minn:
                    if k not in flights[i][j]['airports']:flights[i][j]['airports'][k]={'airlines':{},'7freq':0}
                    flights[i][j]['airports'][k]['7freq']=len(mdg.loc[i].loc[j].loc[k])/d
                    for l in mdg.loc[i].loc[j].loc[k].index.get_level_values(0).unique():
                        if len(mdg.loc[i].loc[j].loc[k].loc[l])>minn: 
                            if l not in flights[i][j]['airports'][k]['airlines']:flights[i][j]['airports'][k]['airlines'][l]={'7freq':0}
                            flights[i][j]['airports'][k]['airlines'][l]['7freq']=len(mdg.loc[i].loc[j].loc[k].loc[l])/d

manual fix TGM - all flights are departing from CLJ, therefore doublecounting + BUD not represented


In [47]:
flights['TGM']['Budapest']=flights['CLJ']['Budapest']

In [48]:
for j in flights['TGM']:
    if flights['CLJ'][j]['7freq']-flights['TGM'][j]['7freq']>0:
        flights['CLJ'][j]['7freq']-=flights['TGM'][j]['7freq']
        ap=list(flights['TGM'][j]['airports'].keys())[0]
        flights['CLJ'][j]['airports'][ap]['7freq']-=flights['TGM'][j]['7freq']
        flights['CLJ'][j]['airports'][ap]['airlines'][u'Wizz Air']['7freq']-=flights['TGM'][j]['7freq']
    else: flights['CLJ'].pop(j)

In [49]:
file("flights_ro.json",'w').write(json.dumps(flights))