2017-06-20 1 views
2

これはしばらく私を悩ませていました。どうすれば=INDEX(A:A,MATCH(E1&F1,B:B&C:C,0))をPythonで実現できますか?これが見つからなければ、エラーを返します。はパンダのインデックスマッチに秀でています

だから私はpd.merge_asofでプレイし始めました。しかし、いずれにしてもエラーを返すだけです。

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

がエラーを与える:

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

編集:

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',]}) 

は、だから、私は単純に上記の式を使用してExcelでこれを行うと、このような何かを得ることができるサンプル、です:

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 

パンダでExcelのINDEX MATCH公式を達成するための簡単な方法は?

+0

あなたはいくつかのサンプルの入力データを掲示し、結果を期待することはできますか? –

+0

@ScottBoston確かに、私に瞬間を与えてください:-) – destinychoice

+0

@ScottBoston私はサンプルで質問を更新しました。遅れて申し訳ありません、持ち去られました。 – destinychoice

答えて

1

のはhow='left'mergeを試してみましょう:

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

出力:

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 
関連する問題