如何比较第一个数据框和数据框2中column_name的黑名单值?
我想从df2返回与黑名单值匹配的行。
df1
column_name blacklist_value
test90.v_6 931
test90.v_7 912
和
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
我希望结果返回与df1中的blacklist_value值匹配的df2数据帧。
答案 0 :(得分:0)
如果您要将黑名单应用于一个特定的列,则看起来像是:
df2[df2['V_1'].isin(list(df1['Blacklist']))]
如果要将其应用于所有列,则外观如下:
df2[df2.isin(list(df1['Blacklist'])).any(axis='columns')]
您可以使用以下数据进行测试:
df1 = pd.DataFrame({
'Blacklist': [931, 912, 950]
})
df2 = pd.DataFrame({
'V_1': [925, 931, 932, 923, 920],
'V_7': [951, 953, 952, 950, 952]
})
如果只需要将其应用于其中一列,则还可以使用merge或join与how ='left'并从黑名单df中选择要加入的列不是na的行(df [〜df [ join_column] .isna()],但我认为这样比较容易。