Home > Blockchain >  for while loop with counter python pandas
for while loop with counter python pandas

Time:03-11

I have a data frame where the index is vendors and there is a column for different months for the order quantity ordered by them for that given month.

Vendor January order qty February order qty
abc 2478 3567

The variables are vendor which is a list containing the name of all the vendors, col which is a list of the names of the columns.

The output desired is the list of vendors for each month that have order quantity below 1. The loop should go through each vendor and append to a list the name of the vendor if it meets the condition. The loop should go through the range of vendor items and after it has gone through the vendor list, 1 will be added to the counter so the next column name is targeted. This is repeated until the end of the column list is reached. I am relatively new to python/pandas so my code could need some work. It was running for 5 mins so I assumed something was not correct with it.

The below is an example of code targeting the January, February and March column.For some reason only jan list works correctly.

jan= []
feb=[]
march=[]
c= 0 

for i in range(0, len(vendor)):
    if c==0:
        if df.loc[vendor[i], col[c]]< 1:
            jan.append(vendor[i])
            if vendor[i]== len(vendor):
                c =1
    elif c==1:
        if df.loc[vendor[i], col[c]]<1:
            feb.append(vendor[i])
            if vendor[i]== len(vendor):
                c =1
    elif c==2:
        if df.loc[vendor[i], col[c]]<1:
            march.append(vendor[i])
            if vendor[i]== len(vendor):
                c =1
                

CodePudding user response:

it's better to share the data frame with us.

I guess c=0 always.

CodePudding user response:

I don't know if this is the most efficient/elegant approach but it works. Suggestions for improvement are appreciated. I ended up using itertools to create a list of tuples for each vendor, month combination:

import itertools

for i in range(0, len(col_list_ord_qty)):
    if i ==0:
        ord_qty_jan = list(zip(vendors, itertools.repeat(col_list_ord_qty[0])))
    elif i ==1:
         ord_qty_feb = list(zip(vendors, itertools.repeat(col_list_ord_qty[1])))
    elif i ==2:
         ord_qty_march = list(zip(vendors, itertools.repeat(col_list_ord_qty[2])))
    elif i ==3:
         ord_qty_april = list(zip(vendors, itertools.repeat(col_list_ord_qty[3])))
    elif i ==4:
         ord_qty_may = list(zip(vendors, itertools.repeat(col_list_ord_qty[4])))
    elif i ==5:
         ord_qty_june = list(zip(vendors, itertools.repeat(col_list_ord_qty[5])))
    elif i ==6:
         ord_qty_july = list(zip(vendors, itertools.repeat(col_list_ord_qty[6])))
    elif i ==7:
         ord_qty_aug = list(zip(vendors, itertools.repeat(col_list_ord_qty[7])))
    elif i ==8:
         ord_qty_sept = list(zip(vendors, itertools.repeat(col_list_ord_qty[8])))
    elif i ==9:
         ord_qty_oct = list(zip(vendors, itertools.repeat(col_list_ord_qty[9])))
    elif i ==10:
         ord_qty_nov = list(zip(vendors, itertools.repeat(col_list_ord_qty[10])))
    elif i ==11:
         ord_qty_dec = list(zip(vendors, itertools.repeat(col_list_ord_qty[11])))

Then I ran a loop to check below 1 order quantity values(one loop :

low_ord_qty_jan =[]
for i in range(0, len(ord_qty_jan)):
     if df_ord_qty.loc[ord_qty_jan[i]] <1:
          low_ord_qty_jan.append(ord_qty_jan[i])
          jan_low_vendors = [idx for idx, val in low_ord_qty_jan]
  • Related