简体   繁体   中英

Kept getting Index Errors with pandas data frame, now can't get correct values

I am having trouble with pandas dataframe. I not able to get my values to appear correctly. I kept getting index errors but now I got my table to show and the inputs are incorrect. This is what I am getting: 在此处输入图片说明 I would like the second line showing 15,0,0 for Andy. Expected output: 在此处输入图片说明

customers = [u'Becky Buyer - Address Two', u'Andy Buyer - Andy nguyen']
plu_parts = [12834L, 11521L, 11164L]
cases = numpy.zeros(shape=(len(plu_parts),len(customers)))

Here I wrote:

 for i in range(len(plu_parts)):
        for j in range(len(customers)):
            plu_sets = [x for x in obs_plu_code_buyer if x['seller_inventory__plu_code_id'] == plu_parts[i] and x['purchase_order_line__purchase_order__buyer__company_name'] + str(" - ") + x['purchase_order_line__purchase_order__delivery_address__contact'] == customers[j]]
            print(plu_sets)
            if len(plu_sets) > 0:
                cases[i][j] = int(plu_sets[0]['num_cases'])

Which gives me

cases =  [[ 100.   10.]
 [  13.    0.]
 [  50.    0.]]



veggies = [u'Conventional Any Custom Mix , 90% Iceberg w/ carrot and red cabbage , 4/5#', u'Conventional Cabbage , Green , 45#', u'Conventional Cabbage , Napa , 15#']


dd  = 'Delivery Dated ' + delivery_date.strftime('%m/%d/%Y')
py = pd.DataFrame({dd :customers})
for k in range(0,len(veggies)):
    d = [int(row) for row in cases[:,0]]
    py.insert(k+1,veggies[k],d[k])

All help is appreciated. EDIT: Included reprex code

Use .T to transpose the numpy array cases upon creation of your dataframe:


customers = [u'Becky Buyer - Address Two', u'Andy Buyer - Andy nguyen']
plu_parts = ['12834L', '11521L', '11164L']
cases = np.array([[100., 10.],
       [13., 0.],
       [50., 0.]])
veggies = [u'Conventional Any Custom Mix , 90% Iceberg w/ carrot and red cabbage , 4/5#', u'Conventional Cabbage , Green , 45#', u'Conventional Cabbage , Napa , 15#']
dd  = 'Delivery Dated 10/01/2020'
df = pd.DataFrame(cases.T, columns=veggies, index=customers)
df.index.name = dd
df

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM