excels index match for pandas

时间:2017-06-20 12:34:46

标签: python pandas

This been bugging me for a while now. How can I achieve =INDEX(A:A,MATCH(E1&F1,B:B&C:C,0))in python? This will return an error if not found.

So I started playing with the pd.merge_asof. But either way I try it only returns errors.

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

Would give the error:

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

So that's a sample, where I could simply do this in excel with above formula and get something like this:

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

So, is there an easy way to achieve the INDEX MATCH formula in excel in pandas?

1 个答案:

答案 0 :(得分:1)

让我们merge尝试how='left'

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