'Function df.empty returning an empty dataframe even when it's not
I have a function in my script that checks if the user is already registred in the DataFrame. But I'm having a problem, sometimes this function simply doesn't work, it works in one file but in the other doesn't. This is the function:
def is_registered(ID): #OK
df = read_any_csv(users_path)
x = df.loc[df["ID"] == ID]
if x.empty:
return False
else:
return True
This function is inside another, called "start()" that is my main code, which will first check if the user is registred or not, if it's not it will ask the name and other informations from the user:
def start():
#Se o usuario estiver registrado, da as boas vindas a ele, caso nao, registra ele e depois da as boas vindas
if is_registered(ID) == True: #OK
current_user = users_df.loc[users_df["ID"] == ID]
name = current_user["NAME"]
name2 =(name.to_string(index=False))
section = int(current_user["SECTION"])
print(f"Ola, {name2}, bem vindo ao programa de treinamento Mercedes Benz Brasil!\n")
videos = videos_to_watch(section)
if is_list_empty(videos,section) == True:
return None
else:
print("Esses sao os tutoriais que faltam serem assistidos:\n")
print(*videos,sep = '\n')
print("Para escolher o tutorial, digite o numero correspondente!\n")
tutorial = show_index_tutorial(videos)
t_path = file_path(section,tutorial)
return t_path
else: #OK
register(ID)
current_user = users_df.loc[users_df["ID"] == ID]
name = current_user["NAME"]
name2 =(name.to_string(index=False))
section = int(current_user["SECTION"])
print(f"Ola, {name2}, bem vindo ao programa de treinamento Mercedes Benz Brasil!\n")
videos = videos_to_watch(section)
print("Esses sao os tutoriais que faltam serem assistidos:\n")
print(*videos,sep = '\n')
print("Para escolher o tutorial, digite o numero correspondente!\n")
tutorial = show_index_tutorial(videos)
t_path = file_path(section,tutorial)
return t_path
When I try it in the file that these two functions were written, it works fine. However, when I try it in the file that I will run my script, it returns False. Below is what it returns in the main file:
users_df = pd.read_csv(users_path)
watched_videos_df = pd.read_csv(watched_videos_path)
x = is_registered("ABCDE")
print(x)
output : True
But in the main file, that's the return:
users_df = pd.read_csv(users_path)
watched_videos_df = pd.read_csv(watched_videos_path)
ID = read_ID()
x = start()
output :
Digite o ID: abcde
Escreva seu nome:
I also noticed that when I run the script first in the file where the functions were written it works fine on the others, but I want it to work even when it's not executed in this file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
