You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于多条件过滤Pandas DataFrame返回空数据框的问题排查

多条件过滤DataFrame返回空结果的问题

我在使用多条件过滤Pandas DataFrame时遇到问题:通过用户输入的变量进行过滤后,返回空DataFrame,但直接把变量替换成具体值就能正常获取对应行。

DataFrame结构示例:

IDCategoryNamePrice
101VehicleCar50000
102GadgetsPhone1000
101GadgetsLaptop2000

我的代码:

user_ID_existing = input("Enter User ID:")
print("Available categories are:\n Vehicle\tGadgets")
user_Category_existing = str(input("Choose from the above category:"))
info = pd.read_excel("Test.xlsx")
data = pd.DataFrame(info)
df = data[((data.ID == user_ID_existing) & (data.Category == user_Category_existing))]
print(df)

我还尝试用numpy实现过滤,同样返回空数据框:

filtered_values = np.where((data['ID'] == user_ID_existing) & (data['Category'].str.contains(user_Category_existing)))
print(filtered_values)
print(data.loc[filtered_values])

内容的提问来源于stack exchange,提问作者Bharath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 04:10:14