Pandas数据帧不断出现索引错误,现在无法获得正确的值

2024-05-18 14:49:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据帧有问题。我无法正确显示我的值。我不断得到索引错误,但现在我得到了我的表显示和输入是不正确的。 这就是我得到的:enter image description here 我想要第二行显示安迪的15,0,0。 预期产出: enter image description here

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)))

我在这里写道:

 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'])

这让我

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])

感谢所有的帮助。 编辑:包含reprex代码


Tags: inforlensetsorderrangebuyerpurchase
1条回答
网友
1楼 · 发布于 2024-05-18 14:49:53

在创建数据帧时,使用.T转置numpy数组cases


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

enter image description here

相关问题 更多 >

    热门问题