Search code examples
pythonpandasdataframecontains

How can I determine that the value of column1 in df1 contains in column1 in df2 (python)


I have df1 image link and have df2 image link

My question how is it possible to determine if df1['whitelist'] contains df2[blocked].

Desirable result: Image link

I was trying:

df2['white_list_num']=df2.apply(lambda x: 0 if x['blocked'] in df1['whitelist'] else 'no num',axis=1)

df1['yes/no'] = [df1["whitelist"].str.find(i) for i in df2['blocked'].to_list()]
df1

but they don't work

Desirable result: Image link


Solution

  • According to my understanding and your desired output, you want to check if whitelist contains blocked even if it is partially containing it. This can be used:

    import pandas as pd
    
    df1 = pd.DataFrame({'whitelist': [1567891112, 1781305891, 2358911121]})
    df2 = pd.DataFrame({'blocked': [156789111, 178130, 23589]})
    
    # checking if whitelist contains blocked values even partially and if so, replacing them with the full whitelist value
    df2['whitelist'] = df2['blocked'].apply(lambda x: df1['whitelist'][df1['whitelist'].astype(str).str.contains(str(x))].values[0] if len(df1['whitelist'][df1['whitelist'].astype(str).str.contains(str(x))].values) > 0 else x)
    

    Output:

         blocked   whitelist
    0  156789111  1567891112
    1     178130  1781305891
    2      23589  2358911121
    

    Works well even on smaller partial values.