gpt4 book ai didi

python - Pandas "if X not in DataFrame"总是抛出错误

转载 作者:行者123 更新时间:2023-12-04 15:00:55 26 4
gpt4 key购买 nike

我有一个提示输入客户 ID 的简单应用。该应用程序使用输入来执行 SQL 语句,然后将结果集转换为数据框。

然后我检查提供的 ID 是否存在于数据框中;但是,即使 ID 确实存在,它也总是返回 false。打印输入和数据框确认输入的 ID 和数据框中的 ID 相同,列名也正确。

while True:
customer_id = input("Insert an existing Customer ID")
print(df)
print(customer_id)
customer_exists = customer_id in df.ID
print(customer_exists)
if not customer_exists:
retry = input("No such customer. Try again? Y/N")
if retry.upper() == 'Y':
continue
else:
return None
else:
break

输出:

#The prompt
Insert an existing Customer ID: A1

#The dataframe
ID FNAME MNAME LNAME ADDRESS1 CUSTOMER_TYPE
0 A1 HELGA None PATHAKI MICHIGAN BRONZE

#The ID that was input by the user
A1

#the boolean result
False

这是怎么回事?

最佳答案

根据数据量,我建议你使用 np.isin()

import numpy as np

customer_id = input("Insert an existing Customer ID") #A1
sample= np.isin(customer_id, item2)
print(sample) # returns True

关于python - Pandas "if X not in DataFrame"总是抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66960661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com