si potrebbe incorrere in e rrors se la colonna non indicizzata contiene celle con NaN.
print df1
Team Year foo
0 Hawks 2001 5
1 Hawks 2004 4
2 Nets 1987 3
3 Nets 1988 6
4 Nets 2001 8
5 Nets 2000 10
6 Heat 2004 6
7 Pacers 2003 12
8 Problem 2112 NaN
print df2
Team Year foo
0 Pacers 2003 12
1 Heat 2004 6
2 Nets 1988 6
3 Problem 2112 NaN
new = df1.merge(df2,on=['Team','Year'],how='left')
print new[new.foo_y.isnull()]
Team Year foo_x foo_y
0 Hawks 2001 5 NaN
1 Hawks 2004 4 NaN
2 Nets 1987 3 NaN
4 Nets 2001 8 NaN
5 Nets 2000 10 NaN
6 Problem 2112 NaN NaN
Il team di problemi nel 2112 non ha alcun valore per foo in nessuna tabella. Quindi, il join sinistro qui restituirà erroneamente quella riga, che corrisponde in entrambi i DataFrames, in quanto non presente nel DataFrame corretto.
Soluzione:
Quello che faccio è quello di aggiungere una colonna unica per il dataframe interno ed impostare un valore per tutte le righe. Quindi quando ti unisci, puoi verificare se quella colonna è NaN per la tabella interna per trovare i record univoci nella tabella esterna.
df2['in_df2']='yes'
print df2
Team Year foo in_df2
0 Pacers 2003 12 yes
1 Heat 2004 6 yes
2 Nets 1988 6 yes
3 Problem 2112 NaN yes
new = df1.merge(df2,on=['Team','Year'],how='left')
print new[new.in_df2.isnull()]
Team Year foo_x foo_y in_df1 in_df2
0 Hawks 2001 5 NaN yes NaN
1 Hawks 2004 4 NaN yes NaN
2 Nets 1987 3 NaN yes NaN
4 Nets 2001 8 NaN yes NaN
5 Nets 2000 10 NaN yes NaN
NB. La riga del problema è ora filtrata correttamente, perché ha un valore per in_df2.
Problem 2112 NaN NaN yes yes
how = 'left'? sicuramente questo non è quello che vuoi (dato il tuo punteggio SO deve essere più complesso di quello) –
L'unione sinistra o destra mi dà un frame di dati che contiene le righe che sono presenti in uno dei frame di dati. Ma ho bisogno di un frame di dati che contenga righe presenti in un frame di dati E NON presenti in un altro. – Roman
Se si tratta di una sola chiave di fusione, è possibile farlo con 'isin' e' ~ ' –