2017-04-16 7 views
1

Ich habe zwei Datenrahmen: 1) Enthält eine Liste von Lieferanten und deren Breite, Länge KoordinatenIterieren über mehrere Datenrahmen Pandas

sup_essential = pd.DataFrame({'supplier': ['A','B','C'], 
           'coords': [(51.1235,-0.3453),(52.1245,-0.3423),(53.1235,-1.4553)]}) 

2) Eine Liste von Geschäften und deren lat, long Koordinaten

stores_essential = pd.DataFrame({'storekey': [1,2,3], 
           'coords': [(54.1235,-0.6553),(49.1245,-1.3423),(50.1235,-1.8553)]}) 

Ich möchte eine Ausgabetabelle erstellen, die Folgendes enthält: Geschäft, Geschäftskoordinaten, Lieferant, Lieferantenkoordinaten, Entfernung für jede Kombination aus Geschäft und Lieferant.

Ich habe derzeit:

test=[] 
for row in sup_essential.iterrows(): 
    for row in stores_essential.iterrows(): 
     r = sup_essential['supplier'],stores_essential['storeKey'] 
     test.append(r) 

Aber das gibt nur ich wiederholt alle Werte

+0

Bitte geben kleine (3-7 Zeilen) reproduzierbare Datensatz in Text/CSV-Format und die gewünschten Datensatz. Lesen Sie bitte [wie man gute reproduzierbare Pandas Beispiele macht] (http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – MaxU

+0

@MaxU Daten selbst ist vertraulich und gegeben, es ist Koordinaten es wäre ziemlich leicht zu identifizieren. Allerdings sind die Header sind ist: Für Geschäfte: storeKey (int) \t locationLongitude \t locationLatitude \t coords (lat, long) Für Lieferanten: Lieferanten (varchar) \t Breite \t Länge \t coords (lat, long) – PaddyD15

+0

Sie müssen die realen Daten nicht angeben. Just [post] (http://stackoverflow.com/posts/43435657/edit) Beispiel (gefälschte) Datensätze in Ihrer Frage – MaxU

Antwort

0

Quelle DFs

In [105]: sup 
Out[105]: 
       coords supplier 
0 (51.1235, -0.3453)  A 
1 (52.1245, -0.3423)  B 
2 (53.1235, -1.4553)  C 

In [106]: stores 
Out[106]: 
       coords storekey 
0 (54.1235, -0.6553)   1 
1 (49.1245, -1.3423)   2 
2 (50.1235, -1.8553)   3 

Lösungen:

from sklearn.neighbors import DistanceMetric 
dist = DistanceMetric.get_metric('haversine') 

m = pd.merge(sup.assign(x=0), stores.assign(x=0), on='x', suffixes=['1','2']).drop('x',1) 

d1 = sup[['coords']].assign(lat=sup.coords.str[0], lon=sup.coords.str[1]).drop('coords',1) 
d2 = stores[['coords']].assign(lat=stores.coords.str[0], lon=stores.coords.str[1]).drop('coords',1) 

m['dist_km'] = np.ravel(dist.pairwise(np.radians(d1), np.radians(d2)) * 6367) 
## -- End pasted text -- 

Ergebnis:

In [135]: m 
Out[135]: 
       coords1 supplier    coords2 storekey  dist_km 
0 (51.1235, -0.3453)  A (54.1235, -0.6553)   1 334.029670 
1 (51.1235, -0.3453)  A (49.1245, -1.3423)   2 233.213416 
2 (51.1235, -0.3453)  A (50.1235, -1.8553)   3 153.880680 
3 (52.1245, -0.3423)  B (54.1235, -0.6553)   1 223.116901 
4 (52.1245, -0.3423)  B (49.1245, -1.3423)   2 340.738587 
5 (52.1245, -0.3423)  B (50.1235, -1.8553)   3 246.116984 
6 (53.1235, -1.4553)  C (54.1235, -0.6553)   1 122.997130 
7 (53.1235, -1.4553)  C (49.1245, -1.3423)   2 444.459052 
8 (53.1235, -1.4553)  C (50.1235, -1.8553)   3 334.514028 
Verwandte Themen