python - pandas isin() function with lists -
i have 2 data frames , want use
isin() method
to check exists in df1 , in df2.
when compare 1 column (index1) - result ok - 2 values of df1.index1 exists in df2.index1.
but when compare 2 columns (index1, index2) of both df1 , df2 result false 3 rows, there 1 common row (index1 = 3, index2 = 6).
why that?
thanks.
in [2]: import pandas pd in [3]: df1 = pd.dataframe({'a': [1,2,3], 'index1':[1,2,3], 'index2':[4,5,6]}) in [4]: df2 = pd.dataframe({'a': [4,5,6], 'index1':[2,3,4], 'index2':[7,6,5]}) in [5]: df1 out[5]: index1 index2 0 1 1 4 1 2 2 5 2 3 3 6 in [6]: df2 out[6]: index1 index2 0 4 2 7 1 5 3 6 2 6 4 5 in [7]: df1['index1'].isin(df2['index1']) out[7]: 0 false 1 true 2 true name: index1, dtype: bool in [8]: df1['index2'].isin(df2['index2']) out[8]: 0 false 1 true 2 true name: index2, dtype: bool in [9]: df1[['index1','index2']].isin(df2[['index1', 'index2']]) out[9]: index1 index2 0 false false 1 false false 2 false false
it's because "the result true @ location if labels match." (docs). if want ignore labels, need use underlying numpy array:
df1[['index1','index2']].isin(df2[['index1', 'index2']].values.ravel()) out: index1 index2 0 false true 1 true true 2 true true
if want labels match columns, can use dictionary:
df1[['index1','index2']].isin(df2[['index1', 'index2']].to_dict('l')) out: index1 index2 0 false false 1 true true 2 true true
if want check whether rows match, need use merge:
df1.merge(df2, how='left', on=['index1', 'index2'], indicator=true) out: a_x index1 index2 a_y _merge 0 1 1 4 nan left_only 1 2 2 5 nan left_only 2 3 3 6 5.0 both
the _merge
column returns both
when both index1 , index2 match.
Comments
Post a Comment