-3

How would I compare the blacklist value for the column_name from the first dataframe to data frame 2?

I want to return the rows from df2 which match the black list value.



df1

column_name blacklist_value
  test90.v_6           931
  test90.v_7           912

and

df2
test90.sno test90.v_1 test90.v_2 test90.v_3 test90.v_4 test90.v_5  \
0          0    7.52785        100       22.2       47.8         13   
1          1    7.43006        100       22.2       47.8       12.9   
2          2    7.40669        100       22.2       47.8         13   
3          3    7.52365        100       22.2       47.9         13   
4          4    7.43734        100       22.3       47.6       13.3   

 test90.v_6 test90.v_7 test90.v_8 test90.v_9 test90.label  
0        925        951        938        954            0  
1        931        953        935        950            0  
2        932        952        937        950            0  
3        923        950        942        950            0  
4        920        952        945        954            0  

I expect the result to return df2 dataframe which match the values of the blacklist_value from df1.

kusshal
  • 1
  • 3

1 Answers1

0

If you want to apply the blacklist to one specific column, it would look like:

df2[df2['V_1'].isin(list(df1['Blacklist']))]

If you want to apply it to all columns, it would look like:

df2[df2.isin(list(df1['Blacklist'])).any(axis='columns')]

You can test it with the following data:

df1 = pd.DataFrame({
'Blacklist': [931, 912, 950]
})

df2 = pd.DataFrame({
'V_1': [925, 931, 932, 923, 920],
'V_7': [951, 953, 952, 950, 952]
})

If you just need to apply it to one of the columns, you could also use merge or join with how='left' and pick the rows where your joined column from your blacklist df is not na (df[~df[join_column].isna()], but I think it is easier this way.

jottbe
  • 4,228
  • 1
  • 15
  • 31