用Pandas.DataFrame.any()匹配字符串

I have a loop which goes through tags on a website for product in products. Each tag contains a string which I scrape and reference product.text. I have an if condition inside the loop checking to see if my DataFrame already contains this product.text str.

if product.text == existing_products_file['Product_ID'].any():

If True, I continue the loop and start at the next product. If False, the rest of the loop is executed.

I can not get the if condition to work properly. I have tried numerous solutions including, but not limited to: turning my DataFrame into a Series and using .isin() by passing a list containing each product.text; looping through the existing_products_file, assigning each value to a variable, and then looping the if condition through each variable; I have also tried rewriting the section entirely, but to no avail.

如果您对问题的根源有任何建议,或者对于达到相同结果的另一种书写方式,请告诉我。

完整的代码和上下文如下:

product_overview = driver.find_element_by_class_name('products') product_box = product_overview.find_elements_by_class_name('product-number') existing_products_file = pd.read_excel('path to excel doc', index=0, usecols=[1]) # for product in product_box: Do something if product.text == existing_products_file['Product_ID'].any(): Do another thing continue else: Do something else

  • I MUST reference my excel sheet that contains already-stored product IDs
  • existing_products_file is a DataFrame consisting of an index and column titled, 'Product_ID'
  • existing_products_file has no Nan values and each product.text is not a Nan
  • I want the if condition to pass True if the product.text str matches any str in existing_products_file column 'Product_ID'
  • I am using Selenium to scrape the website

我有任何遗漏的地方或需要进一步澄清的地方,请告诉我。谢谢!