我的
weight_class
过滤器不适用,我尝试专门针对 weight_class==10924
进行过滤,然后将 dtype 转换为“weight_class”,我还剥离了列,但仍然得到了未过滤重量等级的大米产品
# Fill missing values with zeros in the original DataFrame
df.fillna(0, inplace=True)
df['weight_class'] = df['weight_class'].astype(int)
# Define the grocery basket
grocery_basket = ['Loaf of white bread', 'Sunflower oil', 'Maize meal', 'White sugar', 'Full cream milk - fresh', 'Rice', 'Cake flour', 'Bar Of Bath Soap', 'Spaghetti', 'Peanut butter', 'Cereals', 'Rooibos tea']
# Filter the DataFrame
selected_items = df[(df['product'].isin(grocery_basket)) | ((df['product'] == 'Rice') & (df['weight_class'] != 10917))]
# Print the selected items
print(selected_items)
以下是结果摘录:
prod_id product weight_class Dec-20 Jan-21 Feb-21 \ 68 12131006 Bar Of Bath Soap 10851 12.22 11.92 12.24 74 1212002 Rooibos tea 10855 46.83 46.87 46.78 89 1152003 Peanut butter 10884 32.84 33.32 33.64 96 1113001 Spaghetti 10895 15.85 16.47 16.60 99 1116005 Cereals 10895 41.45 42.28 39.33 108 1112001 Loaf of white bread 10902 15.21 15.63 15.81 117 1111001 Rice 10917 28.14 29.89 30.06 132 1111001 Rice 10924 39.42 42.28 43.41 136 1116001 Cake flour 10926 29.37 29.12 29.17 138 1116003 Maize meal 10926 0.00 0.00 0.00 141 1181001 White sugar 10926 44.74 43.49 45.31 142 1116003 Maize meal 10931 0.00 0.00 0.00 152 1141001 Full cream milk - fresh 10963 28.90 28.79 29.27
期望结果显示
selected_items
,其中一颗大米的 weight_class
为 10924
如果没有其他产品
weight_class
等于10917,可以过滤掉。
selected_items = df.loc[(df['product'].isin(grocery_basket)) & (df['weight_class'] != 10917)]