2017-06-20 6 views
2

Das nervte mich schon eine Weile. Wie kann ich =INDEX(A:A,MATCH(E1&F1,B:B&C:C,0)) in Python erreichen? Dies wird einen Fehler zurückgeben, wenn nicht gefunden wird.excels index match für pandas

So begann ich mit der pd.merge_asof zu spielen. Aber wie auch immer ich versuche, es gibt nur Fehler zurück.

df_3 = pd.merge_asof(df_1, df_2, on=['x', 'y'], allow_exact_matches=False) 

Würde den Fehler geben:

pandas.tools.merge.MergeError: can only asof on a key for left 

Edit:

import pandas as pd 

df_1 = pd.DataFrame({'x': ['1', '1', '2', '2', '3', '3', '4', '5', '5', '5'], 
        'y': ['smth1', 'smth2', 'smth1', 'smth2', 'smth1', 'smth2', 'smth1', 'smth1', 'smth2', 'smth3']}) 
df_2 = pd.DataFrame({'x': ['1', '2', '2', '3', '4', '5', '5'], 
        'y': ['smth1','smth1','smth2','smth3','smth1','smth1','smth3'], 
        'z': ['other1','other1','other2','other3','other1','other1','other3',]}) 

Damit ist ein Beispiel, wo ich einfach diese mit obiger Formel in Excel tun könnte, und so etwas wie diese:

x y  z 
1 smth1 other1 
1 smth2 #NA 
2 smth1 other1 
2 smth2 other2 
3 smth1 #NA 
3 smth2 #NA 
4 smth1 other1 
5 smth1 other1 
5 smth2 #NA 
5 smth3 other3 

Also, gibt es eine n einfachen Weg, um die INDEX MATCH Formel in Excel in Pandas zu erreichen?

+0

Können Sie einige Beispieleingangsdaten veröffentlichen und Ergebnisse zu erwarten? –

+0

@ScottBoston sicher, gib mir einen Moment :-) – destinychoice

+0

@ScottBoston Ich aktualisierte die Frage mit einem Beispiel. Entschuldigung für die Verspätung, wurde weggetragen. – destinychoice

Antwort

1

versuchen Let merge mit how='left':

df_1.merge(df_2, on=['x','y'], how='left') 

Ausgang:

x  y  z 
0 1 smth1 other1 
1 1 smth2  NaN 
2 2 smth1 other1 
3 2 smth2 other2 
4 3 smth1  NaN 
5 3 smth2  NaN 
6 4 smth1 other1 
7 5 smth1 other1 
8 5 smth2  NaN 
9 5 smth3 other3