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

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

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

In [74]:
mdf_dest=pd.read_json(json.loads(file('mdf_hu_dest.json','r').read()))
mdf_arrv=pd.read_json(json.loads(file('mdf_hu_arrv.json','r').read()))

In [75]:
citysave_dest=json.loads(file('citysave_hu_dest.json','r').read())
citysave_arrv=json.loads(file('citysave_hu_arrv.json','r').read())

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


Out[76]:
Airline Airport City Date From To index ID
0 Wizz Air LTN London https://www.airportia.com/hungary/debrecen-int... DEB London LTN 0 DEB
1 Wizz Air EIN Eindhoven https://www.airportia.com/hungary/debrecen-int... DEB Eindhoven EIN 1 DEB
10 Lufthansa MUC Munich https://www.airportia.com/hungary/debrecen-int... DEB Munich MUC 1 DEB
100 Lufthansa MUC Munich https://www.airportia.com/hungary/budapest-lis... BUD Munich MUC 68 BUD
1000 Brussels Airlines BRU Brussels https://www.airportia.com/hungary/budapest-lis... BUD Brussels BRU 92 BUD

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


Out[77]:
Airline Airport City Date From To index ID
0 Lufthansa MUC Munich https://www.airportia.com/hungary/debrecen-int... Munich MUC DEB 0 DEB
1 Wizz Air BGY Milan https://www.airportia.com/hungary/debrecen-int... Milan BGY DEB 1 DEB
10 Wizz Air EIN Eindhoven https://www.airportia.com/hungary/debrecen-int... Eindhoven EIN DEB 1 DEB
100 EasyJet GVA Geneva https://www.airportia.com/hungary/budapest-lis... Geneva GVA BUD 68 BUD
1000 Germanwings DUS Dusseldorf https://www.airportia.com/hungary/budapest-lis... Dusseldorf DUS BUD 79 BUD

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

In [79]:
len(mdf_dest)


Out[79]:
1572

In [80]:
len(mdf_arrv)


Out[80]:
1584

In [81]:
mdf


Out[81]:
Airline Airport City Date From To index ID
0 Wizz Air LTN London https://www.airportia.com/hungary/debrecen-int... DEB London LTN 0 DEB
1 Wizz Air EIN Eindhoven https://www.airportia.com/hungary/debrecen-int... DEB Eindhoven EIN 1 DEB
10 Lufthansa MUC Munich https://www.airportia.com/hungary/debrecen-int... DEB Munich MUC 1 DEB
100 Lufthansa MUC Munich https://www.airportia.com/hungary/budapest-lis... BUD Munich MUC 68 BUD
1000 Brussels Airlines BRU Brussels https://www.airportia.com/hungary/budapest-lis... BUD Brussels BRU 92 BUD
1001 British Airways LHR London https://www.airportia.com/hungary/budapest-lis... BUD London LHR 93 BUD
1002 Qatar Airways DOH Doha https://www.airportia.com/hungary/budapest-lis... BUD Doha DOH 94 BUD
1003 Ryanair CIA Rome https://www.airportia.com/hungary/budapest-lis... BUD Rome CIA 95 BUD
1004 Wizz Air CRL Brussels https://www.airportia.com/hungary/budapest-lis... BUD Brussels CRL 96 BUD
1005 Jet2 EMA East Midlands https://www.airportia.com/hungary/budapest-lis... BUD East Midlands EMA 97 BUD
1006 Austrian Airlines VIE Vienna https://www.airportia.com/hungary/budapest-lis... BUD Vienna VIE 98 BUD
1007 Wizz Air LTN London https://www.airportia.com/hungary/budapest-lis... BUD London LTN 99 BUD
1008 Lufthansa FRA Frankfurt https://www.airportia.com/hungary/budapest-lis... BUD Frankfurt FRA 100 BUD
1009 Air Berlin TXL Berlin https://www.airportia.com/hungary/budapest-lis... BUD Berlin TXL 101 BUD
101 BH Air CDG Paris https://www.airportia.com/hungary/budapest-lis... BUD Paris CDG 69 BUD
1010 Jet2 LBA Leeds https://www.airportia.com/hungary/budapest-lis... BUD Leeds LBA 102 BUD
1011 Wizz Air NCE Nice https://www.airportia.com/hungary/budapest-lis... BUD Nice NCE 103 BUD
1012 Czech Airlines PRG Prague https://www.airportia.com/hungary/budapest-lis... BUD Prague PRG 104 BUD
1013 Wizz Air MXP Milan https://www.airportia.com/hungary/budapest-lis... BUD Milan MXP 105 BUD
1014 Ryanair ATH Athens https://www.airportia.com/hungary/budapest-lis... BUD Athens ATH 106 BUD
1015 Germanwings STR Stuttgart https://www.airportia.com/hungary/budapest-lis... BUD Stuttgart STR 107 BUD
1016 Ryanair BCN Barcelona https://www.airportia.com/hungary/budapest-lis... BUD Barcelona BCN 108 BUD
1017 Swiss ZRH Zurich https://www.airportia.com/hungary/budapest-lis... BUD Zurich ZRH 109 BUD
1018 Air China PEK Beijing https://www.airportia.com/hungary/budapest-lis... BUD Beijing PEK 110 BUD
1019 Transavia ORY Paris https://www.airportia.com/hungary/budapest-lis... BUD Paris ORY 111 BUD
102 Wizz Air AGP Malaga https://www.airportia.com/hungary/budapest-lis... BUD Malaga AGP 70 BUD
1020 Ryanair CRL Brussels https://www.airportia.com/hungary/budapest-lis... BUD Brussels CRL 112 BUD
1021 Eurowings DUS Dusseldorf https://www.airportia.com/hungary/budapest-lis... BUD Dusseldorf DUS 113 BUD
1022 Turkish Airlines IST Istanbul https://www.airportia.com/hungary/budapest-lis... BUD Istanbul IST 114 BUD
1023 Lufthansa MUC Munich https://www.airportia.com/hungary/budapest-lis... BUD Munich MUC 115 BUD
... ... ... ... ... ... ... ... ...
972 EasyJet SXF Berlin https://www.airportia.com/hungary/budapest-lis... Berlin SXF BUD 51 BUD
973 EasyJet GVA Geneva https://www.airportia.com/hungary/budapest-lis... Geneva GVA BUD 52 BUD
974 Lufthansa MUC Munich https://www.airportia.com/hungary/budapest-lis... Munich MUC BUD 53 BUD
975 Air France CDG Paris https://www.airportia.com/hungary/budapest-lis... Paris CDG BUD 54 BUD
976 EasyJet LGW London https://www.airportia.com/hungary/budapest-lis... London LGW BUD 55 BUD
977 Wizz Air FCO Rome https://www.airportia.com/hungary/budapest-lis... Rome FCO BUD 56 BUD
978 KLM AMS Amsterdam https://www.airportia.com/hungary/budapest-lis... Amsterdam AMS BUD 57 BUD
979 Wizz Air CLJ Cluj-Napoca https://www.airportia.com/hungary/budapest-lis... Cluj-Napoca CLJ BUD 58 BUD
98 EasyJet CDG Paris https://www.airportia.com/hungary/budapest-lis... Paris CDG BUD 66 BUD
980 Ryanair BRS Bristol https://www.airportia.com/hungary/budapest-lis... Bristol BRS BUD 59 BUD
981 LOT WAW Warsaw https://www.airportia.com/hungary/budapest-lis... Warsaw WAW BUD 60 BUD
982 Ryanair BLL Billund https://www.airportia.com/hungary/budapest-lis... Billund BLL BUD 61 BUD
983 Alitalia FCO Rome https://www.airportia.com/hungary/budapest-lis... Rome FCO BUD 62 BUD
984 Cargolux HKG Hong Kong https://www.airportia.com/hungary/budapest-lis... Hong Kong HKG BUD 63 BUD
985 British Airways LHR London https://www.airportia.com/hungary/budapest-lis... London LHR BUD 64 BUD
986 Brussels Airlines BRU Brussels https://www.airportia.com/hungary/budapest-lis... Brussels BRU BUD 65 BUD
987 Wizz Air MLA Luqa https://www.airportia.com/hungary/budapest-lis... Luqa MLA BUD 66 BUD
988 Lufthansa FRA Frankfurt https://www.airportia.com/hungary/budapest-lis... Frankfurt FRA BUD 67 BUD
989 Wizz Air LTN London https://www.airportia.com/hungary/budapest-lis... London LTN BUD 68 BUD
99 Ryanair BVA Paris https://www.airportia.com/hungary/budapest-lis... Paris BVA BUD 67 BUD
990 Austrian Airlines VIE Vienna https://www.airportia.com/hungary/budapest-lis... Vienna VIE BUD 69 BUD
991 Finnair HEL Helsinki https://www.airportia.com/hungary/budapest-lis... Helsinki HEL BUD 70 BUD
992 Air Berlin TXL Berlin https://www.airportia.com/hungary/budapest-lis... Berlin TXL BUD 71 BUD
993 Wizz Air HHN Frankfurt https://www.airportia.com/hungary/budapest-lis... Frankfurt HHN BUD 72 BUD
994 Wizz Air SOF Sofia https://www.airportia.com/hungary/budapest-lis... Sofia SOF BUD 73 BUD
995 Swiss ZRH Zurich https://www.airportia.com/hungary/budapest-lis... Zurich ZRH BUD 74 BUD
996 Tarom OTP Bucharest https://www.airportia.com/hungary/budapest-lis... Bucharest OTP BUD 75 BUD
997 Eurowings STR Stuttgart https://www.airportia.com/hungary/budapest-lis... Stuttgart STR BUD 76 BUD
998 Czech Airlines PRG Prague https://www.airportia.com/hungary/budapest-lis... Prague PRG BUD 77 BUD
999 Turkish Airlines IST Istanbul https://www.airportia.com/hungary/budapest-lis... Istanbul IST BUD 78 BUD

3156 rows × 8 columns


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

In [83]:
k=mdg.loc['BUD'].loc['Frankfurt'].loc['FRA']
testurl=u'https://www.airportia.com/hungary/budapest-liszt-ferenc-international-airport/departures/20170318'
k[k['Date']==testurl]


Out[83]:
Date From To index
Airline
Lufthansa https://www.airportia.com/hungary/budapest-lis... BUD Frankfurt FRA 79
Lufthansa https://www.airportia.com/hungary/budapest-lis... BUD Frankfurt FRA 12
Lufthansa https://www.airportia.com/hungary/budapest-lis... BUD Frankfurt FRA 30
Lufthansa https://www.airportia.com/hungary/budapest-lis... BUD Frankfurt FRA 57

In [84]:
k=mdg.loc['BUD'].loc['Frankfurt'].loc['FRA']
testurl=u'https://www.airportia.com/hungary/budapest-liszt-ferenc-international-airport/arrivals/20170318'
k[k['Date']==testurl]


Out[84]:
Date From To index
Airline
Lufthansa https://www.airportia.com/hungary/budapest-lis... Frankfurt FRA BUD 14
Lufthansa https://www.airportia.com/hungary/budapest-lis... Frankfurt FRA BUD 41
Lufthansa https://www.airportia.com/hungary/budapest-lis... Frankfurt FRA BUD 61
Lufthansa https://www.airportia.com/hungary/budapest-lis... Frankfurt FRA BUD 79

In [85]:
k=mdg.loc['BUD'].loc['Frankfurt'].loc['FRA']
for i in range(11,25):
    testurl=u'https://www.airportia.com/hungary/budapest-liszt-ferenc-international-airport/departures/201703'+str(i)
    print 'BUD-FRA March',i, 'departures',len(k[k['Date']==testurl]),
    testurl=u'https://www.airportia.com/hungary/budapest-liszt-ferenc-international-airport/arrivals/201703'+str(i)
    print 'arrivals', len(k[k['Date']==testurl])


BUD-FRA March 11 departures 4 arrivals 4
BUD-FRA March 12 departures 4 arrivals 4
BUD-FRA March 13 departures 4 arrivals 4
BUD-FRA March 14 departures 4 arrivals 4
BUD-FRA March 15 departures 4 arrivals 4
BUD-FRA March 16 departures 4 arrivals 4
BUD-FRA March 17 departures 4 arrivals 4
BUD-FRA March 18 departures 4 arrivals 4
BUD-FRA March 19 departures 4 arrivals 4
BUD-FRA March 20 departures 4 arrivals 4
BUD-FRA March 21 departures 4 arrivals 4
BUD-FRA March 22 departures 4 arrivals 4
BUD-FRA March 23 departures 4 arrivals 4
BUD-FRA March 24 departures 4 arrivals 4

In [88]:
len(k)/14


Out[88]:
8

mdg checks out with source


In [89]:
flights={}
minn=1.0 #want to see minimum 1 flight in the past 2 weeks
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 at least 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():
                        try:
                            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
                        except:pass

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