I wanted to make a function using python to check whether a number (obtained based on a file string) is exist or not in an excel using pandas, if the number exist it will do something like "... is registered" and do some function, and vice versa.
The code that i tried to use is like this:
from tkinter import filedialog
import pandas as pd
import re
file = filedialog.askopenfilename(initialdir="C:/", title="choose file", filetypes=(("xlsx", "*.xlsx"), ("xls", "*.xls")))
name = (r'/(\w+)_(\w+)_excel.xlsx')
number = re.search(name, file).group(2)
references = pd.read_excel(r'C:/example.xlsx')
if float(number) == references:
print(number + " is registered") #and more function
else:
print(number + " is not registered") #and more function
When i used this code it will return ValueError:
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I looked up for it a little and found that (as far as i understand) this happen because the function is not specific enough and can return both true or false, and then i tried using .any or .all after the references in the function but somehow it keeps returning false even if the number does exist in excel.
When i tried to check it using print(number == references) it will show a list and it would tell me where the matching data is found, but somehow when i tried to make a function out of it, it keeps returning false.
The references excel just contains a "Title" in cell A1 and the rest is just the number registered in it.
I tried look for it and found something about pandas.DataFrame.isin but i dont understand how to put it as a function and i dont think i can use it on my problem.
I am sorry if this is a fairly easy question but somehow i cannot find the answer for this, also i am new in python and english is not my first language.