Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do right join where key is null in python pandas

Tags:

python

pandas

Is it possible to do a right join where key is null in python pandas. That is, can I join DateFrames to produce only values from the right that do not match the left?

like image 753
Chet Meinzer Avatar asked Nov 01 '22 08:11

Chet Meinzer


1 Answers

I think this is best expressed as an index selection operation. To find all indices in one frame and not in another, try using the - operator on the two Dataframe index objects, as if the index objects were built-in python set objects. for example:

In [1]: dfa = pd.DataFrame({'A': range(5)}, index=range(5))

In [2]: dfb = pd.DataFrame({'A': range(10, 15)}, index=range(3,8))

In [3]: dfa
Out[3]: 
   A
0  0
1  1
2  2
3  3
4  4

In [4]: dfb
Out[4]: 
    A
3  10
4  11
5  12
6  13
7  14

In [5]: dfb.loc[set(dfb.index) - set(dfa.index)]
Out[5]: 
    A
5  12
6  13
7  14
like image 152
Garrett Avatar answered Nov 09 '22 09:11

Garrett