Home > Back-end >  Why "NameError: name 'product_id_list' is not defined"=
Why "NameError: name 'product_id_list' is not defined"=

Time:10-27

I write this and i don't know why product_id_list is not defined if i have defined it like 4 lines before. Any suggestions? I thin identation is alright so I don't have any more ideas and I also searched around without luck.

Thank you!!

def make_dataSet_rowWise(reorder_product):
    print('unique Product in dataset = ', len(reorder_product.product_id.unique()))
    print('unique order_id in dataset = ', len(reorder_product.order_id.unique()))
    product_id_list = reorder_product.product_id.unique().tolist()
    product_id_list.append("order_id")
    product_id_dict = {}
    i = 0
    
for prod_id in product_id_list:
    product_id_dict[prod_id] = i
    i = i 1 
    product_id_df = pd.Dataframe(columns = product_id_list)
    row_list_all = []
    order_id_list = reorder_product.order_id.unique()
    i = 1
for id in order_id_list:
        #print(i)
    i = i 1
    np_zeros = np.zeros(shape = [len(product_id_list)-1])
    ordered_product_list = reorder_product.loc[reorder_product.order_id == id]["product_id"].tolist()
for order_prod in ordered_product_list:
    np_zeros[product_id_dict.get(order_prod)] = 1
    row_list = np_zeros.tolist()
    row_list.append(id)
    row_list_all.append(row_list)
return (row_list_all, product_id_list)

df_row_wise = make_dataSet_rowWise(reorder_product_99Pct)
product_id_df = pd.DataFrame(df_row_wise[0], columns = df_row_wise[1])
product_id_df.head()

The error I have is this one:

NameError                                 Traceback (most recent call last)
<ipython-input-343-07bcac1b3b48> in <module>
      7     i = 0
      8 
----> 9 for prod_id in product_id_list:
     10     product_id_dict[prod_id] = i
     11     i = i 1

NameError: name 'product_id_list' is not defined

CodePudding user response:

As already mentioned by the other answers, your indentation is wrong. My recommendation is that you use a IDE like VSCode, there is also a free web version enter image description here

There are also wrong indentations with the 3 for loops. The correct indentation should be as the following

enter image description here

CodePudding user response:

I think your indentation may be wrong, the for-loops and return statement is out of the function (with your indentation) so I indented it so that it would still be part of the function...

def make_dataSet_rowWise(reorder_product):
    print('unique Product in dataset = ', len(reorder_product.product_id.unique()))
    print('unique order_id in dataset = ', len(reorder_product.order_id.unique()))
    product_id_list = reorder_product.product_id.unique().tolist()
    product_id_list.append("order_id")
    product_id_dict = {}
    i = 0
    
    for prod_id in product_id_list:
        product_id_dict[prod_id] = i
        i = i 1 
        product_id_df = pd.Dataframe(columns = product_id_list)
        row_list_all = []
        order_id_list = reorder_product.order_id.unique()
        i = 1
        for id in order_id_list:
                #print(i)
            i = i 1
            np_zeros = id.zeros(shape = [len(product_id_list)-1])
            ordered_product_list = reorder_product.loc[reorder_product.order_id == id]["product_id"].tolist()
            for order_prod in ordered_product_list:
                np_zeros[product_id_dict.get(order_prod)] = 1
                row_list = np_zeros.tolist()
                row_list.append(id)
                row_list_all.append(row_list)
    return (row_list_all, product_id_list)

CodePudding user response:

I'm new here, but i think you either need to define the variable out of the scope of

def make_dataSet_rowWise(reorder_product):

OR indent the for loops to be inside

make_dataSet_rowWise
  • Related