Search code examples
pythonstringpandashashstring-search

How search for items in list1 in a second list2 and return a data-frame with items that are equal


Python, How search for items in list1 in a second list2 and return a data-frame with items that are equal.

Anyone would help, I need to find what values are the some in both strings, then return the id related to the row.

Following how I failed trying to return in another dataframe the values that is equal.

This is reference list1 (binglist)

binglist

['31664745', '283494006', '283494005', '283494009', '283494007', 
'283494008','283703957', '283703955', '283703956', '283703954',
 '283703960', '31454872', '283536236', '0', '0', '0', '0', '0',
 '0', '0', '0']

This is list that we need to search on, list2 (Data-frame)

cw["campaignname"].unique()

cw["campaignname"]

array(['35119190', '31664745', '4899110', '804530544', '325772660',
       '283494005', '64002140', '272351300', '2016404066', '753857250',
       '6.12855E+12', '283703956', '283703960',
       '169278078', 'business', '636589579', '52106838', 'science',
       '820812876', 'art'], dtype=object)

Following how I failed trying to return in another dataframe the values that is equal.

# using a set makes the later `x in keep` test faster
keep= set(binglist)


# -> Loop all cw["campaignname"] records only(Bing records)  
# -> If there is a equal value 
# -> return the id 

b = [x= cw["id"] for x in cw["campaignname"].filter(["Bing", "BingBrand", "BingNonBrand"])  if x in bing]

# it give me empty result

it give me empty result


Solution

  • We usually using isin with loc

    Yourdf=cw[cw['campaignname'].isin(keep)].copy()
    Yourdf['id']